【PHP】Detailed operation of PHP files

PHP is a widely used server-side scripting language for developing web applications. In PHP, file operations are an important function, including file reading, writing, deletion, and other operations. This article will detail every aspect of PHP file manipulation and illustrate it with sample code.

1. File reading

To read a file, use PHP's fopen() function to open the file, and then use the fread() function to read the contents of the file. Here is a simple sample code:

$file = fopen("example.txt", "r"); // 打开文件只读模式
if ($file) {
    
    
    $content = fread($file, filesize("example.txt")); // 读取文件内容
    fclose($file); // 关闭文件
    echo $content; // 输出文件内容
} else {
    
    
    echo "无法打开文件";
}

In the above code, we use the fopen() function to open a file named example.txt and assign it to the variable $file. Here we use "r" mode to indicate read-only mode. If the file is successfully opened, the file contents can be read using the fread() function. The first parameter of this function is the file pointer and the second parameter is the number of bytes to read, here we use the filesize() function to get the file size as the number of bytes. When reading is complete, close the file using the fclose() function.

2. File writing

To write to a file, use PHP's fopen() function to open the file, and then use the fwrite() function to write the content to the file. Here is a simple sample code:

$content = "这是要写入文件的内容";
$file = fopen("example.txt", "w"); // 打开文件写入模式
if ($file) {
    
    
    fwrite($file, $content); // 将内容写入文件
    fclose($file); // 关闭文件
    echo "内容已写入文件";
} else {
    
    
    echo "无法打开文件";
}

In the above code, we first define a string variable content which contains the content to be written to the file. Then use the fopen ( ) function to open a file named example.txt and assign it to the variable content, which contains the content to be written to the file. Then use the fopen() function to open a file named example.txt and assign it to the variablecon t e n t , which contains the content to be written to the file . Then use the f o p e n ( ) function to open a file named ex ampl e . t x t , and assign it to the variable file. The first parameter of this function is the file path, and the second parameter is the mode to open the file. Here we use "w" mode to indicate write mode. If the file is opened successfully, you can use the fwrite() function to write content to the file. The first parameter of this function is the file pointer and the second parameter is the string to be written. After writing, close the file using the fclose() function.

3. File deletion

To delete a file, use PHP's unlink() function. Here is a simple sample code:

if (unlink("example.txt")) {
    
    
    echo "文件已删除";
} else {
    
    
    echo "无法删除文件";
}

In the above code, we have deleted the file named example.txt using the unlink() function. The function will return true if the deletion was successful, otherwise false.

Four. Summary

This article details the three aspects of PHP file operations: reading, writing, and deleting. Through the sample code, we can see that PHP provides a wealth of file operation functions and arrays, so that we can easily handle various file operation tasks. In practical applications, we should choose appropriate functions and modes to perform file operations according to specific needs.

Guess you like

Origin blog.csdn.net/qq_22744093/article/details/132476988