File & I / O in PHP
In this article we will learn about explaining file-related functions in PHP.
This chapter explains functions related to files in PHP:
- Open the file
- Read the file
- Write file
- 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 PurposerOpen 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
wOpen 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.
aOpen 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:
- Open the file using the fopen () function.
- Get the file length using the fileize () function.
- Read the file contents using the fread () function.
- 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:
And now the content of tmp.txt will be:
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
You should read it
- Read - Write File in C
- What is ISO file?
- How to open and read the .DAT file?
- What are UDF files?
- What is an XML file and how to open it?
- Read / write File in C ++
- Read the File record in Node.js
- Working with File in Python
- What is a .tmp file? How to open .tmp file on Windows computer?
- What file is M4A? How to open, edit and convert M4A files
- What file is XSD? How to open, edit and convert XSD files
- What file is 3GP? How to open, edit and convert 3GP files