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.

Join the newsletter!

Error: Please check your email address.
Rocket to Success - Your 10 Tips for Smarter ERP System Selection
Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World
Show Comments

Most Popular Reviews

Latest Articles

Resources

PCW Evaluation Team

Ben Ramsden

Sharp PN-40TC1 Huddle Board

Brainstorming, innovation, problem solving, and negotiation have all become much more productive and valuable if people can easily collaborate in real time with minimal friction.

Sarah Ieroianni

Brother QL-820NWB Professional Label Printer

The print quality also does not disappoint, it’s clear, bold, doesn’t smudge and the text is perfectly sized.

Ratchada Dunn

Sharp PN-40TC1 Huddle Board

The Huddle Board’s built in program; Sharp Touch Viewing software allows us to easily manipulate and edit our documents (jpegs and PDFs) all at the same time on the dashboard.

George Khoury

Sharp PN-40TC1 Huddle Board

The biggest perks for me would be that it comes with easy to use and comprehensive programs that make the collaboration process a whole lot more intuitive and organic

David Coyle

Brother PocketJet PJ-773 A4 Portable Thermal Printer

I rate the printer as a 5 out of 5 stars as it has been able to fit seamlessly into my busy and mobile lifestyle.

Kurt Hegetschweiler

Brother PocketJet PJ-773 A4 Portable Thermal Printer

It’s perfect for mobile workers. Just take it out — it’s small enough to sit anywhere — turn it on, load a sheet of paper, and start printing.

Featured Content

Product Launch Showcase

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?