Home > Article > Backend Development > Can copy() in php copy a directory?
copy() cannot copy directories. The copy() function can only copy files. It can copy (copy) a file to the specified directory. If the target file already exists in the specified directory, it will be overwritten; the syntax is "copy (path of the source file to be copied, Target path)", returns TRUE if the copy is successful, and returns FALSE if it fails.
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer
The copy() function cannot copy the directory , this function can only copy files.
сoру() function can copy (copy) a file to the specified directory. It returns TRUE if the execution is successful and FALSE if it fails. The syntax format of the function is as follows:
copy(string $source, string $dest)
The parameter description is as follows:
$source: the path of the source file to be copied;
$dest: target path. If the file exists, it will be overwritten. If $dest is a URL, if the encapsulation protocol does not support overwriting existing files, the copy will fail;
Example: Copy the test.txt file to the img directory and rename it to newtest.txt
<?php header('content-type:text/html;charset=utf-8'); $file = 'test.txt'; $newfile = 'img/newtest.txt'; if(copy($file, $newfile)){ echo '文件复制成功!'; }else{ echo '文件复制失败!'; } ?>
Note:
The copy() function in PHP does not work with remote files. It only works on files accessible to the server's file system.
If the target file already exists, it will be overwritten.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of Can copy() in php copy a directory?. For more information, please follow other related articles on the PHP Chinese website!