move all files in a folder to another?

前端 未结 11 1951
一个人的身影
一个人的身影 2020-12-01 04:01

when moving one file from one location to another i use

rename(\'path/filename\', \'newpath/filename\');

how do you move all files in a fol

相关标签:
11条回答
  • 2020-12-01 04:23

    i move all my .json files from root folder to json folder with this

    foreach (glob("*.json") as $filename) {
        rename($filename,"json/".$filename);
    }
    

    pd: someone 2020?

    0 讨论(0)
  • 2020-12-01 04:24

    An alternate using rename() and with some error checking:

    $srcDir = 'dir1';
    $destDir = 'dir2';
    
    if (file_exists($destDir)) {
      if (is_dir($destDir)) {
        if (is_writable($destDir)) {
          if ($handle = opendir($srcDir)) {
            while (false !== ($file = readdir($handle))) {
              if (is_file($srcDir . '/' . $file)) {
                rename($srcDir . '/' . $file, $destDir . '/' . $file);
              }
            }
            closedir($handle);
          } else {
            echo "$srcDir could not be opened.\n";
          }
        } else {
          echo "$destDir is not writable!\n";
        }
      } else {
        echo "$destDir is not a directory!\n";
      }
    } else {
      echo "$destDir does not exist\n";
    }
    
    0 讨论(0)
  • 2020-12-01 04:30

    As a side note; when you copy files to another folder, their last changed time becomes current timestamp. So you should touch() the new files.

    ... (some codes for directory looping) ...
    if (copy($source.$file, $destination.$file)) {
       $delete[] = $source.$file;
    
       $filetimestamp = filemtime($source.$file); 
       touch($destination.$file,$filetimestamp);
    }
    ... (some codes) ...
    
    0 讨论(0)
  • 2020-12-01 04:31

    tried this one?:

         <?php
    
         $oldfolderpath = "old/folder";
         $newfolderpath = "new/folder";
    
         rename($oldfolderpath,$newfolderpath);
         ?>
    
    0 讨论(0)
  • 2020-12-01 04:33

    try this: rename('path/*', 'newpath/');

    I do not see a point in having an asterisk in the destination

    0 讨论(0)
提交回复
热议问题