Reading and Writing to Files PHP

Communication with files follows the pattern of opening a stream to a file, reading from or writing to it, and then closing the stream. When you open a stream, you get an integer that refers to the open stream. Each time you want to read from or write to the file, you use this stream identifier. Internally PHP uses this integer to refer to all the necessary information for communicating with the file.

To open a file on the local file system, you use the fopen function. It takes a name of a file and a string that defines the mode of communication. This may be r for read-only or w for write-only, among other modes. It is also possible to specify an Internet address by starting the file name with http:// or ftp:// and following it with a full path including a host name.

Two other functions create file streams. You may open a pipe with the popen function or you may open a socket connection with the fsockopen function. If you have much experience with UNIX, you will recognize pipes as temporary streams of data between executing programs. A common Perl method for sending mail is to open a pipe to sendmail, the program for sending mail across the Internet. Because PHP has so many built-in functions, it is rarely necessary to open pipes, but it's nice to know it's an option. You can open a file stream that communicates through TCP/IP with fsockopen. This function takes a hostname and a port and attempts to establish a connection.

Once you have opened a file stream, you can read or write to it using commands like fgets, and fputs. demonstrates this. Notice that a while loop is used to get each line from the example file. It tests for the end of the file with the feof function. When you are finished with a file, end of file or not, you call the fclose function. PHP will clean up the temporary memory it sets aside for tracking an open file.

Keep in mind that PHP scripts execute as a separate user. Frequently this is the "nobody" user. This user probably won't have permission to create files in your Web directories. Take care with allowing your scripts to write in any directory able to be served to remote users. In the simple case where you are saving something like guest book information, you will be allowing anyone to view the entire file. A more serious case occurs when those data files are executed by PHP, which allows remote users to write PHP that could harm your system or steal data. The solution is to place these files outside the Web document tree.

Writing and Reading from a File

<?
/*
** open file for writing
*/
$filename = "data.txt";
if(!($myFile = fopen($filename, "w")))
{
print("Error: ");
print("'$filename' could not be created ");
exit;
}
//write some lines to the file
fputs($myFile, "Save this line for later ");
fputs($myFile, "Save this line too ");
//close the file
fclose($myFile);
/*
** open file for reading
*/
if(!($myFile = fopen($filename, "r")))
{
print("Error:");
print("'$filename' could not be read ");
exit;
}
while(!feof($myFile))
{
//read a line from the file
$myLine = fgets($myFile, 255);
print("$myLine <BR> ");
}
//close the file
fclose($myFile);
?>

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

PHP Topics