PHP file handling

PHP File Processing


Data Access

There are two main ways to access data on the server: File or database

Currently, most applications use databases to read and write data, but file access is still used in some situations, such as:

•      Recording error logs

•  Export data to file

Import data from file

Write file

There are three main steps to write a file, just like we do manually In the same way, the program must follow the following steps:

• Open the file, use the FOPEN function

• Write the file, use the fwrite function

• Close the file, use the FCLOSE function

Open File

fopen() function is used to open a file in PHP.

The first parameter of this function contains the name of the file to be opened, and the second parameter specifies which mode to use to open the file:

<html>
 <body>
 <?php
 $file=fopen("welcome.txt","r");
 ?>
 </body>
 </html>

The file may be opened in the following modes:

QQ图片20161009145912.png

Note: If the fopen() function cannot open the specified file, it returns 0 (false).

Example

If the fopen() function cannot open the specified file, the following example will generate a message:

<html>
 <body>
 <?php
 $file=fopen("welcome.txt","r") or exit("Unable to open file!");
 ?>
 </body>
 </html>

Write file

fwrite () function is used to write files

fwrite also has two main parameters. handle is the Resource variable returned after fopen is opened successfully, and string is the data to be written

int fwrite ( resource $handle , string $string [, int $length ] )

When the writing is successful, fwrite will return the number of characters written. If the writing fails, it will return false

Example

<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "Bill Gates\n";
fwrite($myfile, $txt);
$txt = "Steve Jobs\n";
fwrite($myfile, $txt);
fclose($myfile);
?>

Please note that we wrote to the file "newfile.txt" twice. Each time we write to the file, the string $txt we send contains "Bill Gates" the first time and "Steve Jobs" the second time. After writing is complete, we use the fclose() function to close the file.

If we open the "newfile.txt" file, it should look like this:

Bill Gates

Steve Jobs

Read the file

fread is used to read the entire file without using a while statement to loop. It should be noted that the second parameter is used to control the maximum number of bytes read. This parameter is required.

string fread (resource $handle, int $length)

Execution

echo fread($resource, 1024);

Can read up to 1024 Bytes of file content

Close the file

fclose() function is used to close the open file:

<?php
 $file = fopen("test.txt","r");
 
 //执行一些代码
 
 fclose($file);
 ?>

Detect the end of the file (EOF)

feof() function detects whether the end of file (EOF) has been reached.

The feof() function is useful when looping through data of unknown length.

Note: In w , a and x modes, you cannot read open files!

if (feof($file)) echo "End of file";

Read the file line by line

fgets() function is used to read from Read the file line by line from the file.

Note: After calling this function, the file pointer will move to the next line.

Example

The following example reads a file line by line until the end of the file:

<?php
 $file = fopen("welcome.txt", "r") or exit("无法打开文件!");
 // 读取文件每一行,直到文件结尾
 while(!feof($file))
 {
     echo fgets($file). "<br>";
 }
 fclose($file);
 ?>

Reads the file character by character

## The #fgetc() function is used to read from a file character by character.

Note: After calling this function, the file pointer will move to the next character.

Example

The following example reads the file character by character until the end of the file:

<?php
 $file=fopen("welcome.txt","r") or exit("无法打开文件!");
 while (!feof($file))
 {
     echo fgetc($file);
 }
 fclose($file);
 ?>

file_put_contents

Whether Whether reading or writing, fopen and fclose are very cumbersome calls. Starting from PHP5, a new function

int file_put_contents ( string $filename , mixed $data [, int $flags = 0 [, resource $ context ]] )

file_put_contents is equivalent to executing fopen + fwrite + fclose

file_put_contents('file', "hello world\n");

file_put_contents('file ', "hello world\n", FILE_APPEND);

file_get_contents

string file_get_contents ( string $filename [, bool $use_include_path = false [, resource$context [ , int $offset = -1 [, int $maxlen ]]]] )

file_get_contents is equivalent to executing fopen + fread + fclose

echo file_get_contents('file');

Other file operation functions

S ​​file_exists passes the file address as a parameter to determine whether the file exists, and then true or false

S ​​filesize can return the parameter The size of the file in bytes

S ​​unlink can delete the file represented by the file address passed to it


PHP Filesystem Reference Manual

For a complete reference manual of PHP filesystem functions, please visit our PHP Filesystem Reference Manual.


Continuing Learning
||
<html> <body> <?php $file=fopen("welcome.txt","r"); ?> </body> </html>
submitReset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!