P-Cam
P-Cam

Reputation: 33

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

I have two folders which one of the folders is empty and the other one has 5 images files that named: image1, image2, image3, image4, and image5. I want to move or copy image2, image4, and image5 to another folder at the same time using php. How to copy multiple files from one folder to another with specific file names using php? Please help me with this, thank you in advanced.

Upvotes: 1

Views: 3803

Answers (1)

Rahul Patel
Rahul Patel

Reputation: 5246

The rename function does this

Image rename

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

If you want to keep the existing file on the same place you should use copy

Image copy

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

Use Loop for multiple files as below:

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

Upvotes: 2

Related Questions