PHP File handling concept allow us to do operation with any file in php.
For reading a file we have to open it using fopen() function.Now for reading this file we have a function named as fread().
fread function accept two arguments. First parameter is the file pointer and the other one is the length of the file in form of bytes.
For getting length of file there is filesize() function which takes name of file as an argument and return us the size of file in bytes.
So for reading a file below steps required in PHP.
- Open file by using fopen() function.
- Use filesize() function for Getting the file's length.
- Read the content of file now by using the
fread() function.
- For closing file now use fclose()
function.
Example for reading a file :-
<?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>" );
?>
0 Comment(s)