What is the best practice for uploading multiple images from a folder using PHP?

When uploading multiple images from a folder using PHP, the best practice is to loop through the files in the folder and upload each one individually. This ensures that each image is processed correctly and avoids potential issues with file handling.

$folderPath = 'path/to/folder/';
$files = scandir($folderPath);

foreach($files as $file) {
    if(in_array(pathinfo($file, PATHINFO_EXTENSION), array('jpg', 'jpeg', 'png', 'gif'))) {
        $targetPath = 'path/to/upload/directory/' . $file;
        move_uploaded_file($folderPath . $file, $targetPath);
        echo "File $file uploaded successfully.<br>";
    } else {
        echo "Invalid file format for $file.<br>";
    }
}