Handling files with PHP4 - Part1
July 22, 2002
|
Basic file handling is a fundamental part of programming. Even
now, with powerful and easy to use Database Management Systems
available to manage your files, manipulating files in your own
code is often necessary. Part 1 of this article explains how to
read, write and process files with PHP4.
|
Opening and Closing Files
File handling usually involves three steps. The file is opened,
then put to use (whether reading or writing), and finally closed.
All files should be opened before they are used, and closed when
you've finished with them. Some of the functions do this
automatically, but it's important to get into the habit from the
beginning.
To open a file, you need to know what you're going to be doing
with the file (reading or writing), and of course the name of
the file. The PHP function to do this is fopen, and is
used as follows:
int fopen (string filename, string mode
[, int use_include_path])
Mode can be any of the following:
| r | Opens the file for reading only, starting at
the beginning of the file. |
| r+ | Opens the file for reading and for writing,
starting at the beginning of the file. |
| 'w | Opens the file for writing only. This
overwrites an existing file, or creates a new file. |
| w+ | Opens the file for reading and for writing.
This overwrites an existing file, or creates a new file. |
| a | Opens the file for writing only. This appends
to (starts at the end of) an existing file, or creates a new
one if none is present. |
| a+ | Opens the file for reading for writing. This
appends to (starts at the end of) an existing file, or creates a
new one if none is present. |
| b | Indicates that the file is a binary file (such
as images and sound files, as opposed to text files such as your
PHP scripts). Some operating systems do not distinguish. Windows
does, but Unix doesn't, in which case the flag makes no
difference. |
The fopen function returns an integer called the file handle,
which is used in the other functions we'll look at later, or
false if the open failed for any reason. This means we can use
it in a safe way that deals with errors, as follows:
<?
$filename = "/home/ian/hiddensecrets.txt";
if (!($fp = fopen ($filename, "w"))) {
print "Error - could not open the file $filename";
}
else {
// process the file...
}
?>
Be careful with Windows pathnames. If you use a backslash (\),
you'll need to escape it. You can also use forward slashes (/)
for Windows pathnames.
<?
$filename = "c:\\images\\lion.gif";
if (!($fp = fopen ($filename, "rb"))) {
print "Error - could not open the file $filename";
}
else {
// process the file...
}
?>
PHP also usefully allows you to read files on remote servers.
By beginning the filename with http:// the server connects
to the remote server via HTTP 1.0 in order to open the file.
Similarly, prepending ftp:// opens an ftp connection. The
ftp server must support passive ftp, otherwise the connection
will fail. Note that early versions of PHP4 (before version 4.0.5)
did not support HTTP redirects, which means you'd need to put
the trailing slash at the end of the filename.
The optional parameter at the end of the function indicates
whether PHP should search the directories in the the include path
(set in php.ini). It can be set to '1' or TRUE, otherwise
it defaults to FALSE.
<?
$filename = "http://lionsandtigers.co.za/images/tiger.jpg";
if (!($fp = fopen ($filename, "rb"))) {
print "Error - could not open the file $filename";
}
else {
// process the file...
}
?>
Closing a file is easy - just call the fclose function with file
handler passed to you when the file was opened. Closing files
may seem pointless, especially as you're unlikely to notice a
difference when testing with small scripts like these. Leaving
files open unecessarily just puts extra load on your systems,
and is bound to bite you if you're working on larger systems.
bool fclose (int fp)
<?
$filename = "/home/ian/hiddensecrets.txt";
if (!($fp = fopen ($filename, "w"))) {
print "Error - could not open the file $filename";
}
else {
// process the file...
fclose($fp);
}
?>
Although fclose() returns true or false, most code doesn't
check for the possible failure of this operation unless it's
absolutely critical to know. Besides, the vast majority of
failures occur when opening a file, (invalid filenames or paths
etc.), while the kinds of errors that can cause your successfully
opened file not to close, (disk failures etc.), are not the kind
of thing your code can do much about.
Download the scripts from this article
Displaying files - Page 2
|