Reading & Writing from/to files in php
Written by Codes Tips on March 1, 2009 – 2:20 pm -Description
I want to begin the first post about PHP with something easy, but very useful. Writing and reading to/from files in PHP.
Here is a short description for reading from file in PHP:
- Open the file for reading
- If the open was successful then we can read from the output file
- After we read from file we should close the file.
Here is a short description for writing to files in PHP:
- Open the file for writing
- If the open was successful then we can Write to the output file
- After we wrote to file we should close the file.
OPENING
To open a file in PHP you should use the function fopen:
pointer fopen( string $filepath, string $mode)
This function return a pointer resource when the $filepath exists or FALSE when the file doesn’t exist.
$filepath is the path to the file that we want to open.
$mode - this is the type of access when reading or writing to files
- r - opens the file for reading only
- r+ - open the file for reading/writing and places the file at the begining of the file
- w - opens the file for writing only
- w+ - opens the file for reading/writing places the pointer at the beginning of the file and truncate the size of the file to 0
- a - places the pointer at the end of the file for writing(append)
- a+ - places the pointer at the end of the file for reading and writing(append).
- x - opens for writing, places pointer at the begining of the file.
- x+ - opens for writing/reading, places pointer at the begining of the file.
READING
You can use more functions for reading from file after you open it:
fread($filepointer,$nbytes)
$filepointer - a pointer to the file
$nbytes - number of bytes you wanna read from file. (a character have a size of a byte)
fgets($filepointer) - reads a line from the file
fgetc($filepointer) - reads a caracter from file
WRITING
You can used fwrite($filepointer, $datastring) to write a string to file
CLOSING
After you finished writing/reading from file you shuld use:
fclose($filepointer) - to close the file
Example of codes:
For reading the first 10 caracters from a file you can use the folowing block of code:
$f=fopen("c:\\path\\file.txt","r"); $string = fread($f,10); fclose($f);
Reading an entire file to a string variable character by character:
$f=fopen("c:\\file.txt","r"); $textstring=""; while (!feof($f)) { $c=fgetc($f); $textstring=$textstring.$c; } fclose($f);
Reading a file quickly:
$path = "c:\\file.txt"; $f=fopen($path,"r"); $textstring = fread($f,filesize($path)); fclose($f);
Writing a two lines of string to a file
$path = "myfile.txt"; $f = fopen($path, 'w') or die("error opening the file"); $allstring="this is the first line of the test\nthis is the second line of the test"; fwrite($f,$allstring); fclose($f);
Additional functions that you can be useful for file handling are:
feof($filepointer) - test if the end of file was reached.
file_exists($filepointer) - tests if a a file exists
die() or exit() - terminate the script that is currently executed.
Tags: PHP/MYSQL
Posted in PHP/MYSQL |
Leave a Comment
You must be logged in to post a comment.

















