PHP code to read a text file

Sunday, January 25, 2009

PHP file functions - Read, Write, Append

I have posted about four times on this blog to cover the file functions, to read, write append data to files. This post gives links to each post so that if you are up to learn file reading and writing you can find whole stuff on single place. So here we go.

- PHP code to read text from a file
- PHP code to read single line or read text line by line from text file
- PHP code to create/open file for writing
- PHP Code to append data to a file(write data at end of file)

If you have any more questions about file handling in PHP just direct them to me via comments.


PHP append text to a file

Following php script opens an existing file and appends the text to it. In other words it add the text at the end of file without removing the old data. If in case it don't find file with specified name in current directory, it creates the file with that name in current directory.

If your file is in another directory specify full path on place of file name.




$fh = fopen('fileName.txt', 'a') or die("can't open file"); //open file for appending data
$text = "this is text to be written to file, so hello how are you\n";
fwrite($fh, $text); //write text on the end of file.
fclose($fh);
?>


PHP Create or open a file and write text into it

Following code will help us in creating a file a write text into it, if file with specified name already exists if will overwrite the file data.


$fh = fopen('fileName.txt', 'w') or die("can't open file");
$text = "this is line one\n";
fwrite($fh, $text); //write first time to file
$text = "this is line 2\n";
fwrite($fh, $text);//write second time to file
fclose($fh);
?>

Variable $text may contain many lines.
Remember if file already exists this will remove all old data, to add data to file without removing old data see append data to a file



PHP code to read single line and read line by line text file

fgets is the function which reads single line in a file, and with a loop with this function how this reads single line, it reads the line where currently pointer is.

Read single line on pointer location

$filename="FileNameHere.txt";
$fh=fopen($filename,'r'); //r means we are opening file only for reading
$textLine = fgets($fh); // reading the line where the pointer currently is
fclose($fh); //script closes file
echo $textLine; //displaying text line which is just read from file
?>

Read Whole file line by line and display


$fh = @fopen('fileName.txt', 'r');
if ($fh) {
while (!feof($fh)) { //loop for pointer to go to each line
$buffer = fgets($fh);//reading line
echo $buffer; //displaying line
$buffer1.=$buffer; //storing whole text of file in $buffer1 for any future use
}
fclose($fh);
}
?>


Saturday, January 24, 2009

PHP Script to read a text file

Mostly while working with web some times we need our page to read text from a text file so following is code to read text from a text file.


$filename="FileNameHere.txt";
$fh=fopen($filename,'r'); //r means we are opening file only for reading
$textinfile = fread($fh, filesize($filename)); //fread which takes file handle and file size, reading the file data
fclose($fh); //script closes file
echo $textinfile; //dispalying text which is just read from file
?>

Now variable $textinfile contains whole text/data of file, this can be displayed or used anyway you want.

Posted in |