简体   繁体   中英

How can i move all image files from a directory to a sub-directory using php

I am attempting to move all images from my /webfiles directory to my /webfiles/images directory. I have managed to do it to a single image using the below code:

$imgfiles = glob("webfiles/28.png");
rename($imgfiles[0], "webfiles/images/28.png");

However i have multiple images and the names will be unknown so cannot specify as per the above.

// Get array of all source files
$files = scandir("source");
// Identify directories
$source = "webfiles/";
$destination = "webfiles/images/";
// 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM