If you don't need write access to the file or if the file you need is on a remote server, you can load the entire file into an array, splitting the file at each line break.
$filearray = file("dir/filename");
$filearray = file ("http://www.someothersite.com/dir/file");
if (!($filearray = @file($hostname.$documentname)))
{echo "$fail_message";}
To access the content, process one line at a time.
while (list(,$oneline) = each($filearray)) {
# process the contents of $oneline here
}
For more control over local files, use fopen. This allows the script to open files in a variety of modes - read-only (r) or read+write(r+) which preserve any existing content and start reading/writing at the start of the file; write-only (w) or read+write (w+) which delete all existing content in the file, reading or writing starts with an empty file; append-only (a) or read and append (a+), existing content is preserved and reading or writing starts at the end of the file. With w, w+, a or a+ options, if the file does not exist, PHP will attempt to create it. Make sure the permissions for the directory concerned are set to allow the server to write the file.
fopen("filename","r+");
# read+write, keep content, start at top of file
if (!$fp = @fopen("$dir\$filename","w"))
die ("Couldn't open file $filename");
# write only to an empty file, delete existing content.)
The file is now open, but a separate operation is needed to read or write to the file. PHP has a variety of functions that can read individual characters, sets of a fixed number of characters, or one line at a time. Covering each function in depth is beyond the scope of this site, particularly as it is well covered in PHP texts (see the CodeHelp books page). For now, I will just consider fgets() which can be used to read a specified number of bytes at a time, but which has the flexibility to stop reading when it encounters a new line or the end of the file. Thus, by choosing a length greater than any single line in the file, you can use fgets() to read files one line at a time. The advantage over file() is that fopen() and fgets() can be used together to allow the script to change part of a file. This snippet of code takes content from one file, adds some content at the beginning and then completes the output file. The first step, reading content from the temp.php file, could be done with file(), but fopen() is used here for demonstration purposes. As a result of using fopen(), filesize() is used to find the complete size of the file and ftell() to find out where we are within the file. Also note the nesting of the fopen() commands and the use of two separate file pointers, $fp and $fout to identify the source and output files respectively.
# read temp.php, write tempinclude=1,
# then insert temp.php into final outputfile
# note: not for use on very small, single line files -
# use fgetc() instead
$maxread=filesize("temp.php");
if (!($fout=fopen($outputfile,"w")))
die ("Cannot create output file.");
fwrite($fout,"<?php\n$tempinclude=1;\n?>");
if (!($fp=fopen("temp.php","r")))
die ("Cannot open temporary file.");
$data = fgets($fp,$maxread);
while ((ftell($fp)<$maxread)) {
fwrite($fout,$data);
$data = fgets($fp,$maxread);
}
fclose($fp);
fclose($fout);
By pattern matching each line, an enhanced script could determine where new content should be inserted and a simple fwrite() command could be used to insert the content. This cannot be done with file() alone, as the file is closed as soon as the file() command returns. fclose() is optional - the file will be closed when the script exits. If your script needs to do more processing after completing file access, and particularly if either file opened in the script is to be accessed by another script or user, use fclose($filepointer); to allow other users, services or scripts to access the file.
This is part of www.codehelp.co.uk Copyright © 1998-2004 Neil Williams
See the file about.html for
copying conditions.