PHP Example Tutorial: File Operation Skills Reading and Writing
Introduction:
In the PHP development process, file operations are very common and important One of the tasks. This article will introduce reading and writing techniques in PHP file operations, and provide code examples to help readers get started quickly.
1. Reading files
1.1 fopen() function
Use the fopen() function to open a file and return a file pointer of resource type. This function accepts two parameters: file name and opening mode.
Code example:
$filename = 'example.txt'; $handle = fopen($filename, 'r');
1.2 fread() function
fread() function is used to read data from an open file. It accepts two parameters: file pointer and read number of bytes.
Code example:
$filename = 'example.txt'; $handle = fopen($filename, 'r'); $data = fread($handle, filesize($filename)); fclose($handle);
1.3 fgets() function
fgets() function is used to read data line by line from the file pointer. It accepts one parameter: the file pointer.
Code example:
$handle = fopen('example.txt', 'r'); while (!feof($handle)) { $line = fgets($handle); echo $line; } fclose($handle);
2. Writing files
2.1 fwrite() function
Use the fwrite() function to write data to a file. Accepts two parameters: the file pointer and the data to be written.
Code example:
$handle = fopen('example.txt', 'w'); fwrite($handle, 'Hello, World!'); fclose($handle);
2.2 file_put_contents() function
The file_put_contents() function is used to write data to a file and return the number of bytes written. This function accepts two parameters: the file name and the data to be written.
Code example:
$file = 'example.txt'; $data = 'Hello, World!'; file_put_contents($file, $data);
2.3 fputs() function
fputs() function is used to write a line of data to a file. It accepts two parameters: the file pointer and the file to be written. The data.
Code example:
$handle = fopen('example.txt', 'w'); fputs($handle, 'Hello, World!'); fclose($handle);
3. Exception handling
During the file operation process, some errors or exceptions may occur. In order to increase the robustness of the code, we can use exception handling mechanism to catch and handle these errors.
3.1 try...catch statement
Use the try...catch statement to catch exceptions that may be thrown and handle them accordingly in the catch block.
Code examples:
try { $handle = fopen('example.txt', 'r'); // 进行文件操作 fclose($handle); } catch (Exception $e) { echo '文件操作出错:' . $e->getMessage(); }
Conclusion:
This article introduces the reading and writing skills in PHP file operations and provides corresponding code examples. It is hoped that readers can master the basic skills of PHP file operation and improve development efficiency through studying this article. Of course, file operations involve more complex functions and application scenarios. Readers can further study and practice to improve their file operation capabilities.
The above is the detailed content of PHP example tutorial: file operation skills reading and writing. For more information, please follow other related articles on the PHP Chinese website!