How can I move a file to a specific directory in PHP?

You can use the copy() function in PHP to duplicate files to a specified directory.

Here is an example code:

$sourceFile = 'path/to/source/file.txt';
$destinationDir = 'path/to/destination/';
$destinationFile = $destinationDir . 'file.txt';

if(!file_exists($destinationDir)) {
    mkdir($destinationDir, 0777, true);
}

if(copy($sourceFile, $destinationFile)) {
    echo '文件复制成功';
} else {
    echo '文件复制失败';
}

In the code above, we first specify the path of the source file and the path of the target directory. Then, we use the copy() function to duplicate the source file to the target directory. Before copying the file, we also check if the target directory exists, and if not, we create the directory. Finally, we use the return value of the copy() function to determine if the file was successfully copied.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds