Using FTP in PHP scripts

Using FTP in PHP scripts: Part II

The previous page we looked at basic usage of FTP inside PHP scripts. This covered connecting to an FTP server, listing files, uploading files and downloading files. In this article we will examine advanced FTP features in PHP.

Resuming an FTP download

FTP resumption is a lifesaver to people who regularly move files over the Internet. It works like this: a user is downloading a file from an FTP site. The server, the Internet connection or something else fails. When the user tries to retrieve the file again, the FTP client is in a position to realise that a file by that name has already been downloaded (at least partially). The FTP client can verify whether the files are the same size. If the local file is smaller, the user is asked whether they want to treat the file as a download resumption or download the full file. If the user chooses to resume, the FTP client sends an offset of the number of bytes from the beginning of the file at which to start.

PHP also provides this functionality to developers. This means that you can add fault tolerance and increased efficiency to your FTP-enabled PHP scripts.

Consider the following:

<?
$FILE = "test.txt";
$size = file_exists($FILE) ? filesize($FILE) - 1 : 0;
/* ... */
$remsize = ftp_size($conn,$FILE);
if($remsize == $size + 1) {
    echo "Remote file is the same size as local file\n";
} else if($remsize > $size + 1) {
    if(!ftp_get($conn,$FILE,$FILE,FTP_ASCII,$size)) {
        echo "Could not download $FILE\n";
    }
}
/* ... */
?>

This snippet of code tests whether a local file, $FILE, is the same size as a remote file. If it is the same size, the user is informed. If the remote file is larger than the local file, the script retrieves it. The third line of the script determines the size of the $FILE locally. If $FILE exists, filesize() returns the size in bytes. The script reduces the size returned by 1 byte, since the resumption functionality counts from zero, not one.

If the file does not exist, the script sets the file size to zero bytes.

The example skips over the code involved in connecting and authenticating with an FTP server.

Finally, the script gets the file size of the remote file and compares it to $size. If the two files are equal in size, the user is notified. If the remote file is larger, the script resumes the download at offset $size.

Non-blocking FTP

With the FTP functions we've looked at so far, when a script downloads or uploads a file, the script blocks - that is, it stops executing until the download or upload finishes. Non-blocking code is the opposite: the code continues to execute and the user can test whether or not the network function has finished when convenient. This is very useful if, say, your script has to do things unrelated to an FTP transfer. Those things can be done in parallel to the download. Consider the following:

<?
$FILE = "test.txt";
/* ... */
ftp_nb_get($conn,$FILE,$FILE,FTP_BINARY);
/* ... */
while(($ret = ftp_nb_continue($conn)) == FTP_MOREDATA) {
    sleep(1);
}
if($ret == FTP_FINISHED) {
    echo "Finished downloading $FILE\n";
} else {
    echo "An error was encountered downloading $FILE\n";
}
?>

This script uses ftp_nb_get() to initiate the FTP download. It accepts the same arguments as ftp_get(). However, the function does not block the script - it allows it to execute code in parallel with the download. To check on the status of the download, the script calls ftp_nb_continue().

This returns one of three values: FTP_MOREDATA - there is more data to download; FTP_FINISHED - the download is complete; or, FTP_FAILED - an error was encountered. To force the script to block, we loop while ftp_nb_continue() returns FTP_MOREDATA, sleeping for one second for each iteration.

Finally, when the script breaks out of this loop, the final return value of ftp_nb_continue() is tested to see if the file was downloaded or if an error was encountered.

More information on FTP-related functions can be found at www.php.net/manual/en/ref.ftp.php.

Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World

Comments

siva

1

hi how are

eriogorti

Ballard

2

Preschool teachers education

Good morning. Could you imagine how horrible things would be if we always told others how we felt? Life would be intolerably bearable.
I am from Marino and learning to speak English, please tell me right I wrote the following sentence: "Firstelectricnewspaper registration fee waived for lith preschool."

Thank :) Ballard.

Comments are now closed.

Latest News Articles

Most Popular Articles

Follow Us

GGG Evaluation Team

Kathy Cassidy

STYLISTIC Q702

First impression on unpacking the Q702 test unit was the solid feel and clean, minimalist styling.

Anthony Grifoni

STYLISTIC Q572

For work use, Microsoft Word and Excel programs pre-installed on the device are adequate for preparing short documents.

Steph Mundell

LIFEBOOK UH574

The Fujitsu LifeBook UH574 allowed for great mobility without being obnoxiously heavy or clunky. Its twelve hours of battery life did not disappoint.

Andrew Mitsi

STYLISTIC Q702

The screen was particularly good. It is bright and visible from most angles, however heat is an issue, particularly around the Windows button on the front, and on the back where the battery housing is located.

Simon Harriott

STYLISTIC Q702

My first impression after unboxing the Q702 is that it is a nice looking unit. Styling is somewhat minimalist but very effective. The tablet part, once detached, has a nice weight, and no buttons or switches are located in awkward or intrusive positions.

Resources

Best Deals on GoodGearGuide

Compare & Save

Deals powered by WhistleOut
WhistleOut

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?