This chapter explains functions related to files in PHP:

  1. Open the file
  2. Read the file
  3. Write file
  4. Close the file

Open and close the file in PHP

PHP's fopen () function is used to open a file. It requires 2 parameters, the first parameter is the file name, and the second parameter is mode, ie the mode to operate.

The file's mode can be defined as one of the 6 options in the following table.

Mode Purposer

Open the file and read only.

Move the file pointer to the beginning of the file

r +

Open the file for reading and writing.

Move the cursor to the beginning of the file

w

Open the file and just write.

Move the cursor to the beginning of the file

and cut the file to 0.

If the file does not exist, it will try to create a file.

w +

Open the file for reading and writing.

Move the cursor to the beginning of the file

and cut the file to a length of 0.

If the file does not exist, it will try to create a file.

a

Open the file for the purpose of recording only.

Move the cursor to the end of the file

If the file does not exist, it will try to create a file.

a +

Open the file for the purpose of reading and writing.

Move the cursor to the end of the file

If the file does not exist, it will try to create a file.

If you try to open a failed file, then the fopen function returns a false value, otherwise it returns a file pointer used for continuing to read or write the file.

After making a change to the opened file, it is important to close it now using the fclose () function. The fclose () function requires a file pointer as its parameter and then returns true if the closing is successful and false otherwise.

Read a file in PHP

When a file is opened using the fopen () function, it can be read with a fread () function in PHP. This function requires 2 parameters. They must be file pointers and file lengths in bytes.

The length of the file can be known by using the fileize () function in PHP, it takes the file name as a parameter and returns the size of the file by single byte.

Follow these steps to read a file with PHP:

  1. Open the file using the fopen () function.
  2. Get the file length using the fileize () function.
  3. Read the file contents using the fread () function.
  4. Close the file using the fclose () function.

Suppose you have a file containing content named tmp.txt . If not, create a text file with any content.

Save the program in a file named test.php in htdocs , then open the browser and type the address http:/// localhost: 8080 / test.php to see the result.

The following example assigns the content of a text file to a variable, then displays their content on the web page.

 Đọc file trong PHP  php $filename = "tmp.txt" ; $file = fopen ( $filename , "r" ); if ( $file == false ) { echo ( "Xảy ra lỗi khi mở file!" ); exit (); } $filesize = filesize ( $filename ); $filetext = fread ( $file , $filesize ); fclose ( $file ); echo ( "Kích cỡ của file: $filesize bytes" );
echo ( "Nội dung của file:
" ); echo ( "
 $ filetext 
 " ); ?> 

Save the program in a file named test.php in htdocs , then open the browser and type the address http:/// localhost: 8080 / test.php to see the result.

Write files in PHP

A new file can be written or text can be appended to an existing file using the fwrite () function in PHP. This function requires two parameters: the file pointer and the data string to be written. An optional 3rd integer parameter can be added to determine the length of the data to be written. If the third parameter is added, recording will stop after the specified length has been reached.

The following example creates a new text file, and writes a short text to its head. After closing this file, the existence of this file is confirmed by the file_exist () function, which will receive the file name as a parameter.




Write files in PHP





Save the above program in a file named test.php in htdocs , then open the browser and type the address http:/// localhost: 8080 / test.php will result:

File & I / O in PHP Picture 1

And now the content of tmp.txt will be:

File & I / O in PHP Picture 2

We will discuss all functions related to input and output files in the File Processing Function chapter in PHP .

Follow tutorialspoint

Previous article: Insert file in PHP

Next lesson: PHP function

5 ★ | 2 Vote | 👨 191 Views

Above is an article about: "File & I / O in PHP". Hope this article is useful to you. Don't forget to rate the article, like and share this article with your friends and relatives. Good luck!

« PREV POST
NEXT POST »