php - 将文件夹中的所有文件移动到另一个文件夹中?

标签 php file

当我将一个文件从一个位置移动到另一个位置时

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

如何将一个文件夹中的所有文件移动到另一个文件夹?试过这个没有结果:

rename('path/*', 'newpath/*');

最佳答案

稍微冗长的解决方案:

// 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);
}

关于php - 将文件夹中的所有文件移动到另一个文件夹中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2082138/

相关文章:

c - 函数迭代从文本文件接收到的相同输入

Python 实现比 C 快

php - jQuery 中没有图像宽度和高度

java - 制作一个应用程序来打开您自己的文件扩展名

php - Apache Mod_Rewrite 不断重定向

javascript - 当用户使用调制解调器时获取用户地理位置

php - 如果索引小于某个值,则打印所有数组字符串

php - (显然)奇怪的 "Access denied"MySQL 错误

javascript - 从属下拉列表 - 自动加载

mysql - fputcsv 和 fwrite 不工作