将文件夹中的所有文件移动到另一个? [英] move all files in a folder to another?

查看:121
本文介绍了将文件夹中的所有文件移动到另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将一个文件从一个位置移动到另一个位置时,我使用

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

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

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


解决方案

一个稍微详细的解决方案:

  //获取所有源文件的数组
$ files = scandir(source);
//识别目录
$ source =source /;
$ destination =destination /;
//循环遍历所有源文件
foreach($ files as $ file){
if(in_array($ file,array(。,..)))继续;
//如果我们成功复制,则将其标记为删除
if(copy($ source。$ file,$ destination。$ file)){
$ delete [] = $ source。 $文件;
}
}
//删除所有成功复制的文件
foreach($ delete as $ file){
unlink($ file);
}


when moving one file from one location to another i use

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

how do you move all files in a folder to another folder? tried this one without result:

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

解决方案

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

这篇关于将文件夹中的所有文件移动到另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆