File & I / O 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
May be interested
- What file is M4A? How to open, edit and convert M4A filesthe file is part of an extension .m4a is an mpeg-4 audio file. they are usually found in apple's itunes store and are considered as the default format of songs when downloaded.
- What file is XSD? How to open, edit and convert XSD filesa file with the .xsd extension is most likely an xml schema file. this is a text-based file format, which defines the validation rules for xml files and explains xml forms.
- What are IGS files? How to open the IGS . filea file with the igs extension is most likely an iges drawing file used by cad programs to save vector image data in the ascii text format.
- What is CHD file? How to use CHD file?if you're playing retro games, you may have come across files with the extension '.chd'. let tipsmake.com learn about chd file and how to use it through the following article!
- The Fsutil file in Windowsthe fsutil file finds a file by the user name (if disk quotas is enabled), the query allocates the scope to a file, sets the short name to a file, sets the valid data length of the file, sets the data zero for a file, or create a new file.
- Instructions for creating and using BAT file on Windowshow to create a simple batch file and some basics about it will be presented in this article. at the same time you will be provided with some resources for learning and writing batch files.
- How to create an EXE installation filesometimes to install certain software on your computer, you must use the .exe file, but have you ever wondered what the purpose of this file is and how it was created? in this article, tipsmake.com will answer that question and guide you to create an exe installation file.
- What kind of file is M3U8 file?a file with a .m3u8 extension is a file that contains utf-8 encoded audio playlists
- What is an exe file?a file with the exe extension is an executable file used in operating systems such as windows, ms-dos, openvms, and reactos to open software programs.
- What file is PAT? How to open, edit and convert PAT filesa file with a .pat extension is most likely a sample image file (pattern image file) used by graphics programs to create a template or texture on an image with a smaller and regular image. squarebuilt.