move all files in a folder to another?

前端 未结 11 1986
一个人的身影
一个人的身影 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:18

    A slightly verbose solution:

    // Get array of all source files
    $files = scandir("source");
    // Identify directories
    $source = "source/";
    $destination = "destination/";
    // Cycle through all source files
    foreach ($files as $file) {
      if (in_array($file, array(".",".."))) continue;
      // If we copied this successfully, mark it for deletion
      if (copy($source.$file, $destination.$file)) {
        $delete[] = $source.$file;
      }
    }
    // Delete all successfully-copied files
    foreach ($delete as $file) {
      unlink($file);
    }
    

提交回复
热议问题