Home  >  Article  >  Backend Development  >  About the use of basic functions of PHP operating files

About the use of basic functions of PHP operating files

不言
不言Original
2018-06-21 10:30:461873browse

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(&#39;./file1.txt&#39;,&#39;../data/file2.txt&#39;)){
echo "文件复制成功!";
}else{
echo "文件复制失败!";
}
 
//删除文件示例
$filename = "file.txt";
if (file_exists($filename)){
if (unlink($filename)){
echo "文件删除成功!";
}else{
echo "文件删除失败!";
}
}else{
echo "目标文件不存在";
}
 
//重命名文件示例
if (rename(&#39;./demo.php&#39;, &#39;./demo.html&#39;)){
echo "文件重命名成功!";
}else{
echo "文件重命名失败";
}
 
//截取文件示例
$fp = fopen(&#39;./data.txt&#39;, "r+") or die(&#39;文件打开失败&#39;);
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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn