How to move specific images to a new folder php

I have a folder that contain some images pic1.jpg, pic2.jpg, pic3.jpg, frame2.jpg. round.jpg. My question is how to move only pic*.jpg images to a new folder without adding the other images frame*.jpg and round.jpg using php

Upvotes: 0

Views: 45

Answers (2)

user2560539
user2560539

Reputation:

// search for all the pathnames matching pattern
foreach(glob("/myfolder/pic*.jpg") as $file)
{ 
// check if newfolder exists, if not create it
if(!is_dir('/newfolder') && !file_exists('/newfolder')) {
mkdir('/newfolder', 0755);
}
// new file path
$newfile = '/newfolder/'.basename($file);
// copy file to new location
copy($file, $newfile);
}

Upvotes: 0

Pedro Lobito
Pedro Lobito

Reputation: 98901

Use php glob and basename

foreach(glob("/old/pic*.jpg") as $file)
{
    rename($file, "/new/".basename($file));
}

Upvotes: 1

Related Questions