13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Backing Up or Mirroring a File<br />

467<br />

Avoiding Timeouts<br />

One problem you might face when transferring files via FTP is exceeding the maximum<br />

execution time.You know when this happens because <strong>PHP</strong> gives you an error message.<br />

This error is especially likely to occur if your server is running over a slow or congested<br />

network, or if you are downloading a large file, such as a movie clip.<br />

The default value of the maximum execution time for all <strong>PHP</strong> scripts is defined in<br />

the php.ini file. By default, it’s set to 30 seconds.This is designed to catch scripts that<br />

are running out of control. However, when you are transferring files via FTP, if your link<br />

to the rest of the world is slow or if the file is large, the file transfer could well take<br />

longer than this.<br />

Fortunately, you can modify the maximum execution time for a particular script by<br />

using the set_time_limit() function. Calling this function resets the maximum number<br />

of seconds the script is allowed to run, starting from the time the function is called.<br />

For example, if you call<br />

set_time_limit(90);<br />

the script will be able to run for another 90 seconds from the time the function is called.<br />

Using Other FTP Functions<br />

A number of other FTP functions are useful in <strong>PHP</strong>.The function ftp_size() can tell<br />

you the size of a file on a remote server. It has the following prototype:<br />

int ftp_size(int ftp_connection, string remotefile_path)<br />

This function returns the size of the remote file in bytes or –1 if an error occurs. It is<br />

not supported by all FTP servers.<br />

One h<strong>and</strong>y use of ftp_size() is to work out the maximum execution time to set for<br />

a particular transfer. Given the file size <strong>and</strong> speed of your connection, you can take a<br />

guess as to how long the transfer ought to take <strong>and</strong> use the set_time_limit() function<br />

accordingly.<br />

You can get <strong>and</strong> display a list of files in a directory on a remote FTP server by using<br />

the following code:<br />

$listing = ftp_nlist($conn, dirname($remotefile));<br />

foreach ($listing as $filename)<br />

echo “$filename ”;<br />

This code uses the ftp_nlist() function to get a list of names of files in a particular<br />

directory.<br />

In terms of other FTP functions, almost anything that you can do from an FTP<br />

comm<strong>and</strong> line, you can do with the FTP functions.You can find the specific functions<br />

corresponding to each FTP comm<strong>and</strong> in the <strong>PHP</strong> online manual at http://us2.php.net/<br />

manual/en/ref.ftp.php.<br />

The exception is mget (multiple get), but you can use ftp_nlist() to get a list of<br />

files <strong>and</strong> then fetch them as required.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!