Home >Backend Development >PHP Tutorial >PHP file operation function example: file copy
In Web development, file operation is a very common requirement, and PHP is a very powerful language that also provides a variety of file operation functions to meet this requirement. Among them, file copying is also one of the commonly used functions. This article will introduce the file copy function in PHP file operation function and give example code.
First, let’s take a look at the file copy function in PHP. There are two main methods: copy() and file_put_contents(). Among them, the copy() function is the file copy function officially provided by PHP, while file_put_contents() has a wider range of uses and is also feasible in file copying.
The format of the copy() function is: copy($source_file,$dest_file). Where $source_file is the name of the source file to be copied, and $dest_file is the name of the new file to be copied to. After calling this function, the content contained in $source_file will be copied to $dest_file. If a file already exists in $dest_file, the previous content will be overwritten.
Next, I will provide you with a simple example to illustrate the use of the copy() function:
<?php $source_file = "test.txt"; //源文件名 $dest_file = "copy.txt"; //复制文件名 if(copy($source_file,$dest_file)) { echo "复制成功!"; } else { echo "复制失败!"; } ?>
In the above code, $source_file is the name of the source file to be copied, $dest_file is the name of the new file to be copied to. Through the judgment of the if...else statement, we can determine whether the copy operation is successful.
Compared with the copy() function, the file_put_contents() function is more flexible. It operates not only copying, but also writing , append and other operations. The format is as follows:
file_put_contents($file_name,file_content,[flag],context)
$file_name: The name of the file to write data to. If the file does not exist, it will be automatically created.
$file_content: The data to be written, which can be a string, array, etc.
[flag]: Writing mode identifier, which can be FILE_APPEND, indicating append writing, or omitted, indicating overwriting writing.
context:
Next, I will provide you with an example code of the file_put_contents() function:
<?php $file = "test.txt"; // 源文件名 $dest_file = "copy.txt"; // 复制文件名 $content = file_get_contents($file); // 读取源文件内容 if(file_put_contents($dest_file,$content)) { echo "复制成功!"; } else { echo "复制失败!"; } ?>
In the above code, we first use the file_get_contents function to read the source file $source_file content and assign it to $content, and then call the file_put_contents() function to write the content in $content to $dest_file, thereby realizing file copying.
Conclusion
The above are the two methods of file copying in PHP. I hope it will be helpful to everyone. In the actual development process, different file operation methods can be selected according to different needs. Finally, we should also pay attention to the read and write permissions of the file and the correctness of the file path to avoid errors.
The above is the detailed content of PHP file operation function example: file copy. For more information, please follow other related articles on the PHP Chinese website!