In this tutorial, we will be giving an in-depth explanation to the different PHP functions related to files -
- Opening a File
- Reading a File
- Writing a File
- Closing a File
Opening and Closing a File
The file mode can be specified as one of the six available options in the table below -
Sr.No | Mode & Purpose |
---|---|
1 | r Opens the file for reading only. Places the file pointer at the beginning of the file. |
2 | r+ Opens the file for reading and writing. Places the file pointer at the beginning of the file. |
3 | w Opens the file for writing only. Places the file pointer at the beginning of the file. and truncates the file to zero length. If files does not exist then it attempts to create a file. |
4 | w+ Opens the file for reading and writing only. Places the file pointer at the beginning of the file. and truncates the file to zero length. If files does not exist then it attempts to create a file. |
5 | a Opens the file for writing only. Places the file pointer at the end of the file. If files does not exist then it attempts to create a file. |
6 | a+ Opens the file for reading and writing only. Places the file pointer at the end of the file. If files does not exist then it attempts to create a file. |
After making all the changes to the opened file, it is vital to close it by using the fclose() function. The fclose() requires a file pointer as its argument and then returns true when the closure succeeds and false if it fails.
Reading a File
The file length can be found by using the PHP filesize() function which takes the file name as its argument and returns the size of the file expressed in bytes.
Following are the steps needed to read a file with PHP -
- Open a file using the fopen() function.
- Get the length of the file by using the filesize() function.
- Read the file's content using the fread() function.
- Close the file by making use of the fclose() function.
Example
<html> <head> <title>Reading a file using PHP</title> </head> <body> <?php $filename = "tmp.txt"; $file = fopen( $filename, "r" ); if( $file == false ) { echo ( "Error in opening file" ); exit(); } $filesize = filesize( $filename ); $filetext = fread( $file, $filesize ); fclose( $file ); echo ( "File size : $filesize bytes" ); echo ( "<pre>$filetext</pre>" ); ?> </body> </html>
Output
Writing a File
Example
<?php $filename = "/home/user/guest/newfile.txt"; $file = fopen( $filename, "w" ); if( $file == false ) { echo ( "Error in opening new file" ); exit(); } fwrite( $file, "This is a simple test\n" ); fclose( $file ); ?> <html> <head> <title>Writing a file using PHP</title> </head> <body> <?php $filename = "newfile.txt"; $file = fopen( $filename, "r" ); if( $file == false ) { echo ( "Error in opening file" ); exit(); } $filesize = filesize( $filename ); $filetext = fread( $file, $filesize ); fclose( $file ); echo ( "File size : $filesize bytes" ); echo ( "$filetext" ); echo("file name: $filename"); ?> </body> </html>
Output
Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.