How can PHP be optimized to efficiently handle the task of moving multiple .jpg files to a specific folder?

To efficiently handle the task of moving multiple .jpg files to a specific folder in PHP, we can use a loop to iterate through the list of files and use the `rename()` function to move each file to the desired folder.

$sourceDir = "/path/to/source/directory/";
$destinationDir = "/path/to/destination/directory/";

$files = glob($sourceDir . "*.jpg");

foreach ($files as $file) {
    $fileName = basename($file);
    $destination = $destinationDir . $fileName;
    
    if (rename($file, $destination)) {
        echo "File $fileName moved successfully.\n";
    } else {
        echo "Error moving file $fileName.\n";
    }
}