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";
}
}
Related Questions
- What are the key differences between PHP 7.2 and PHP 5.6 that could cause compatibility issues in scripts?
- What are the potential pitfalls of relying on HTTP_REFERER to determine the previous page in PHP?
- How can PHP arrays be effectively used to organize and display database query results in a structured format?