Specifications

Avoiding Timeouts
One problem you might face when FTPing files is exceeding the maximum execution time.
You will know whether this happens because PHP will give you an error message. This is
especially likely to occur if your server is running over a slow or congested network, or if you
are downloading a large file, such as a movie clip.
The default value of the maximum execution time for all PHP scripts is defined in the php.ini
file. By default, its set to 30 seconds. This is designed to catch scripts that are running out of
control. However, when you are FTPing files, if your link to the rest of the world is slow, or if
the file is large, the file transfer could well take longer than this.
Fortunately, we can modify the maximum execution time for a particular script using the
set_time_limit() function. Calling this function resets the maximum number of seconds the
script is allowed to run, starting from the time the function is called. For example, if you call
set_time_limit(90);
then the script will be able to run for another 90 seconds from the time the function is called.
Using Other FTP Functions
There are a number of other useful FTP functions in PHP.
The function ftp_size() can tell you the size of a file on a remote server. It has the following
prototype:
int ftp_size(int ftp_connection, string remotefile_path)
This function returns the size of the remote file in bytes, or -1 if there is an error. This is not
supported by all FTP servers.
One handy use of ftp_size() is to work out what maximum execution time to set for a partic-
ular transfer. Given the file size and the speed of your connection, you can take a guess as to
how long the transfer ought to take, and use the set_time_limit() function accordingly.
You can get and display a list of files in a directory on a remote FTP server with the following
code:
$listing = ftp_nlist($conn, “$directory_path”);
foreach ($listing as $filename)
echo “$filename <br>”;
This code uses the ftp_nlist() function to get a list of names of files in a particular directory.
Advanced PHP Techniques
P
ART IV
386
22 7842 CH17 3/6/01 3:39 PM Page 386