File Handling in php
What is Filehandling?
readfile() Function
The readfile() function reads a file and writes it to the output buffer.
myfile.txt
File name myfile.txt
Php was originally an acronym for personal home pages. But is now a recursive acronym for php : Hypertext Preprocessor.
php is called hypertext preprocessor as its libraries are already compiled.when any person request for any php page in the address bar of the browser that request is first sent to the server then server interpret php files and return back response in form of Html.so it is called hypertext preprocessor.
File name index.php
<?php
echo readfile("myfile.txt");
?>
How to open file in php
fopen() function
fopen() function is used to open a file in php. this function requires two arguments. first file name and second file permission.
File name index.php
<?php
$mydoc = fopen("myfile.txt", "r") or die("Unable to open file!");
echo fread($mydoc,filesize("myfile.txt"));
fclose($mydoc);
?>
File name index.php
<?php
$fileName = "/doc/myFile.txt";
$fn = fopen($fileName,"r");
if( $fn == false )
{
echo ( "Error in opening file" );
exit();
}
?>
File Reading in php
The fread() function reads from an open file. This function requires two arguments. The first parameter of fread() contains the name of the file to read from and the second parameter specifies the maximum number of bytes to read.
The files size can be calculated using the filesize() function which takes the file name as its argument and returns the size of the file in bytes.
So here are the steps required to read a file with PHP.
Open a file using fopen() function.
Get the file's length using filesize() function.
Read the file's content using fread() function.
Close the file with fclose() function.
File name index.php
<?php
$fileName = "/doc/myFile.txt";
$fp = fopen($fileName,"r");
if( $fp == false )
{
echo ( "Error in opening file" );
exit();
}
$fileSize = filesize( $fileName );
$fileData = fread( $fp, $fileSize );
?>
File name index.php
<?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>" );
?>
fclose()
fclose() function is used to close an open file. fclose() requires the name of the file (or a variable that holds the filename) we want to close:
good programming practice to close all files after you have finished with them.
File name index.php
<?php
$myfile = fopen("itechtuto.txt", "r");
// some code to be executed....
fclose($myfile);
?>
File Writing in php
A new file can be written or text can be appended to an existing file using the PHP fwrite() function. This function requires two arguments specifying a file pointer and the string of data that is to be written. Optionally a third integer argument can be included to specify the length of the data to write. If the third argument is included, writing would will stop after the specified length has been reached.
File name index.php
<?php
$fileName = "/doc/myFile.txt";
$fp = fopen($fileName,"w");
if( $fp == false )
{
echo ( "Error in opening file" );
exit();
}
fwrite( $fp, "This is a sample text to write\n" );
?>
Reading a file Line by line
fgets() function is used to read file line by line.(ie. single line from a file). after that it moved to the next line of file.
File name index.php
<?php
$fileName = "/doc/myFile.txt";
$fp = fopen($fileName,"r");
if( $fp == false )
{
echo ( "Error in opening file" );
exit();
}
while(!feof($fp))
{
echo fgets($fp). "<br>";
}
?>
Check End-Of-File - feof()
The feof() function checks if the "end-of-file" (EOF) has been reached.
The feof() function is useful for looping through data of unknown length.
File name index.php
<?php
$myfile = fopen("itechtuto.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
echo fgets($myfile) . "<br>";
}
fclose($myfile);
?>
Reading single character - fgetc()
The fgetc() function is used to read a single character from a file.
File name index.php
<?php
$myfile = fopen("itechtuto.txt", "r") or die("Unable to open file!");
// Output one character until end-of-file
while(!feof($myfile)) {
echo fgetc($myfile);
}
fclose($myfile);
?>
Previous
Next