如何使用php将多个文件从一个文件夹复制到另一个具有特定文件名的文件夹


How to copy multiple files from one folder to another with specific file names using php?

我有两个文件夹,其中一个文件夹是空的,另一个文件夹有5个图像文件,命名为:image1, image2, image3, image4和image5。我想移动或复制image2, image4和image5到另一个文件夹在同一时间使用php。如何将多个文件从一个文件夹复制到另一个特定的文件名使用php?请帮我这个,提前谢谢你。

重命名函数执行此操作

图片重命名

rename('image2.jpg', 'newfolder/image2.jpg');
rename('image4.jpg', 'newfolder/image4.jpg');
rename('image5.jpg', 'newfolder/image5.jpg');

如果你想保留现有的文件在同一个地方,你应该使用copy

映像拷贝

copy('image2.jpg', 'newfolder/image2.jpg');
copy('image4.jpg', 'newfolder/image4.jpg');
copy('image5.jpg', 'newfolder/image5.jpg');

对多个文件使用循环,如下所示:

//Create an array with image files which should be copy or move in new folder
$files = ['image2.jpg','image4.jpg','image5.jpg'];
foreach($files as $resFile){
    rename($resFile, 'newfolder/'.$resFile);
    copy($resFile, 'newfolder/'.$resFile);
}