How can PHP be used to automate the process of renaming and moving files with different names?
To automate the process of renaming and moving files with different names using PHP, you can use the `rename()` function to rename the file and the `copy()` function to move it to a new location. You can also use variables to store the new file name and destination path to make the process dynamic and handle multiple files.
<?php
$oldFileName = 'old_file.txt';
$newFileName = 'new_file.txt';
$sourcePath = '/path/to/source/directory/';
$destinationPath = '/path/to/destination/directory/';
// Rename the file
rename($sourcePath . $oldFileName, $sourcePath . $newFileName);
// Move the renamed file to the destination directory
copy($sourcePath . $newFileName, $destinationPath . $newFileName);
?>
Keywords
Related Questions
- What best practices should be followed when handling database queries and result sets in PHP to ensure efficient and secure data retrieval and manipulation?
- How can a variable with a leading hashtag (#) be passed via GET in PHP?
- What is the potential issue with using DISTINCT in a SQL query when dealing with multiple table joins in PHP?