To move a file to a different directory in PHP, you can use the rename()
function. Here's an example:
$sourceFilePath = '/path/to/file.ext'; // Path to the original file
$destinationDirectory = '/path/to/destination/'; // Path to the destination directory
$newFilePath = $destinationDirectory . basename($sourceFilePath); // Construct the new file path
if (rename($sourceFilePath, $newFilePath)) {
echo "File moved successfully.";
} else {
echo "File move failed.";
}
In this example, we use the rename()
function to move the file. The first parameter is the path to the original file, and the second parameter is the path to the destination directory concatenated with the original file name (using basename()
function to extract the file name).
If the rename()
function returns true
, it means the file was moved successfully. Otherwise, it returns false
indicating that the move operation failed.