move all files in a folder to another?

前端 未结 11 1991
一个人的身影
一个人的身影 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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";
    }
    

提交回复
热议问题