开发者

How to get a directory content and move to another directory with PHP

开发者 https://www.devze.com 2023-03-24 17:35 出处:网络
I\'ve been trying to get this done with no luck. I\'ve got a php script that downloads and UNZIPS a file. No problem here.

I've been trying to get this done with no luck. I've got a php script that downloads and UNZIPS a file. No problem here.

After that, I need to get the unzipped files and replace all the files of the root directory with the files on this folder. Problem is, the folder is in the root directory.

Eg.: my directory is something like this

/
/folder 1
/folder 2
/folder 3
/file.php
/file2.php
etc...

After I run my download script I unzip the archive to a folder called update in the root of the dir

/update/--UNZIPED STUFF--

I need to delete virtually all files/dirs from the root DIR excluding the update folder I guess and a couple of other files and then move the files/folders inside the update folder to the root of the dir.

Any ideas?

UPDATE

Ok, so I've made a code that seems to be right but I get a lot of file permission to when using unlink or rename. How can I go about that?

Here is the bit of code I've made

    echo '<table>';
    $updateContents = scandir('./');
    foreach($updateContents as $number=>$fileName){
    if($fileName != 'update' && $fileName != 'config.ini' && $fileName != 'layout.php' && $fileName != '..' && $fileName != '.' && $fileName != '.git' && $fileName != '.gitignore'){
        if(is_dir($fileName)){
          moveDownload($fileName, 'old/'.$fileName);
        } else {
          if(rename($fileName, 'old/'.$fileName)){
            echo '<tr><td>'.$fileName.' moved successfully </td><td><font color="green">OK</font></td></tr>';
          } else {
            echo '<tr><td>Could not move file '.$fileName.'</td><td><font color="red">ERROR</font></td></tr>';
          }
        }
      }
    }
    echo '</table>';

and the moveDownload function is one I found here somewhere. It moves a non empty folder and it was slightly modified by me.

function moveDownload($src,$dst){ 
  $dir = opendir($src); 
  while(false !== ($file = readdir($dir))){ 
    if (($file != '.') && ($file != '..') && ($file != 'config.ini') && ($file != 'layout.php') && ($file != 'sbpcache') && ($file !='update')){ 
      if (is_dir($src.'/'.$file)){
        if(file_exists($dst.'/'.$file)){
          rrmdir($dst.'/'.$file);
        }
      }
      if(@rename($src . '/' . $file, $dst . '/' . $file)){
            echo '<tr><td>'.$file.' moved successfully </td><td><font color="green">OK</font></td></tr>';
      } else {
        if(@chmod($src.'/'.$file, 0777)){
          if(@rename($src . '/' . $file, $dst . '/' . $file)){
                echo '<tr><td>'.$file.' moved successfully </td><td><font color="green">OK</font></td></tr>';
          } else {
                echo '<tr><td>Could not move file '.$file.'</td><td><font color="red">ERROR RENAME</font></td></tr>';
          }
        } else {
            echo '<tr><td>Could not move file '.$file.'</td><td><font color="red">ERROR CHMOD</font></td></tr>';
        }
      }
    } 
  } 
  closedir($dir); 
}

I also do the same in the again but moving from the update folder.

So basically, first instead of deleting things I am just moving them to a folder called old and then I t开发者_JAVA技巧ry to move the new things in, but I get permission problems everywhere. Some files move, some don't, even if I have ' sudo chmod 777 -R'. Any ideas?


PHP's opendir will allow you to iterate along all the files and directories inside the root directory. You can use unlink to delete the files or folders and add an if clause to check if the item is to be deleted or not.


Why don't you just get a list of all directories / files under root and then loop through them, delete them (using unlink) while ignoring the "backup" directory. Next you can either run a php copy OR shell_exec mv * command to move the files from backup to root.


  1. what for to use root folder. use /tmp
  2. what the problem with unlink / rmdir
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号