This php code demonstrates in detail how to open a file, read a file and close a file. In php, you can open a file through fopen, get the file size through the filesize function, fread to read the file content, and fclose to close the file
- < html>
- Reading a file using PHP
-
-
- $filename = "/home/user/guest/ 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 ( "
" );
- ?>
-
Copy code
|