This article mainly introduces some basic function usage examples of PHP operating files. This article gives examples of operation codes such as copying files, deleting files, renaming files, intercepting files, etc. Friends who need it can refer to it
When operating on a file, you can not only operate on the data in the file, but also on the file itself. For example, operations such as copying files, deleting files, intercepting files, and renaming files. Standard functions for these file processing methods have been provided in PHP, and they are very easy to use, as shown in the following table:
In the table, if the four functions are executed successfully, TRUE will be returned, and FALSE will be returned on failure. Their usage codes are as follows:
<?php //复制文件示例 if(copy('./file1.txt','../data/file2.txt')){ echo "文件复制成功!"; }else{ echo "文件复制失败!"; } //删除文件示例 $filename = "file.txt"; if (file_exists($filename)){ if (unlink($filename)){ echo "文件删除成功!"; }else{ echo "文件删除失败!"; } }else{ echo "目标文件不存在"; } //重命名文件示例 if (rename('./demo.php', './demo.html')){ echo "文件重命名成功!"; }else{ echo "文件重命名失败"; } //截取文件示例 $fp = fopen('./data.txt', "r+") or die('文件打开失败'); if(ftruncate($fp, 1024)){ echo "文件截取成功!"; }else{ echo "文件截取失败!"; } ?>
The above is the entire content of this article. I hope it will be helpful to everyone’s learning. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
How to use html static pages to call php files
About the analysis of php_pdo preprocessing statements
About PHP directory operations
##
The above is the detailed content of About the use of basic functions of PHP operating files. For more information, please follow other related articles on the PHP Chinese website!