PHP open, read, close file

【Foreword】

      PHP has various functions for manipulating files, including creating, reading, uploading, and editing files.

     This article summarizes file open, read, and close

    

【List】

(1) Open and read a file; (2) Open a file; (3) Read a file; (4) Read a single-line file

(5) Check read line by line; (6) Read a single character; (7) Close the file

 

【Details】

(1) Open and read the file

   The readfile() function reads a file and writes it to the output buffer

If you just want to open a file and read the contents, use the readfile() function

If the reading is successful, the readfile() function returns the number of bytes. I will introduce the knowledge about bytes in the next article.

Case:

<?php
echo readfile("two.txt");
?>

 Here, my two.txt is

Hello

 The output of the operation is: hello 6

 Note: The number of bytes returned here is 6. I will explain the details of the bytes in the next article.

(2) Open the file

    There is also a method to open a file, fopen(filename, mode to open the file), this function can provide more options than the readfile() function

    The first parameter of fopen() contains the name of the file to be opened, and the second parameter specifies the mode of opening the file

The mode in which the file is opened :

r Opens the file as read-only. The file pointer starts at the beginning of the file.

w Opens the file for write only. Delete the contents of the file or create a new one if it does not exist. The file pointer starts at the beginning of the file.

a Open the file for writing only. Existing data in the file is preserved. The file pointer starts at the end of the file. Create a new file if the file does not exist.

x Creates a new file as write-only. Returns FALSE and an error if the file already exists.

r+ opens the file for read/write, the file pointer starts at the beginning of the file.

w+ opens the file for read/write. Delete the file contents or create a new file if it doesn't exist. The file pointer starts at the beginning of the file.

a+ Open file for read/write. Data already in the file is preserved. The file pointer starts at the end of the file. Create a new file if it doesn't exist.

x+ creates new file as read/write. Returns FALSE and an error if the file already exists.

(3) Read the file

fread(name of file to be read, maximum number of bytes to be read) The function reads an open file.

The first parameter contains the filename of the file to be read, and the second parameter specifies the maximum number of bytes to be read. The following PHP code

   ①Read the "webdictionary.txt" file to the end:

fread($myfile,filesize("webdictionary.txt"));

   ②Read only 3 bytes

fread($myfile,3);

(4) Read a single-line file

   The fgets() function is used to read a single line from a file. The following example outputs the first line of the "webdictionary.txt" file, for example:

<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fgets($myfile);
fclose($myfile);
?>

   Note: After calling fgets() function, the file pointer is moved to the next line

(5) Check read line by line

      The feof() function checks whether the "end-of-file" has been reached. It is useful for traversing data of unknown length, and can preserve the lines of the original file by adding a <br> line element.

     The following example reads the "webdictionary.txt" file line by line until the end-of-file, example:

<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// output single line until end-of-file
while(!feof($myfile)) {
  echo fgets($myfile) . "<br>";
}
fclose($myfile);
?>

(6) Read a single character

   The fgetc() function is used to read a single character from a file

   The following example reads the "webdictionary.txt" file character by character until end-of-file, example:

 

<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// output single character until end-of-file
while(!feof($myfile)) {
  echo fgetc($myfile);
}
fclose($myfile);
?>

 

   Note: After calling the fgetc() function, the file pointer is moved to the next character

(7) Close the file

   The fclose() function is used to close an open file, with only one parameter - the name of the file to be closed (or a variable containing the file name)

<?php
$myfile = fopen("webdictionary.txt", "r");
fclose($myfile);
?>

   Note: Open files take up server resources, so it's a good programming practice to close them all when you're done using them

 

 

 

 

 

.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326145779&siteId=291194637