How can one efficiently move and rename a file with an unknown file name in PHP?
To efficiently move and rename a file with an unknown file name in PHP, you can use the `glob()` function to search for the file based on a pattern, such as a specific file extension. Once you have located the file, you can use the `rename()` function to move and rename it to the desired destination.
// Find the file with a specific extension in a directory
$files = glob('/path/to/directory/*.txt');
if (!empty($files)) {
// Get the first file found
$fileToMove = $files[0];
// Define the new file path and name
$newFilePath = '/new/path/new_filename.txt';
// Move and rename the file
if (rename($fileToMove, $newFilePath)) {
echo 'File moved and renamed successfully.';
} else {
echo 'Error moving file.';
}
} else {
echo 'No files found.';
}