- 1
- 2
- < previous
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.
- 1
- 2
- < previous
Discover how SOA can create smarter outcomes for your business.
Attend and learn:
- How SOA is helping leading companies to become more agile
- Where you should be applying SOA processes in your company
- The top SOA implementation mistakes to avoid
Click here for more information.
- +
Computerworld Live Podcast #97: The Future of Enterprise Networking 25/07/2008 09:45:36
This week CW Live chats with Mark Thompson, global sales and marketing manager for HP ProCurve, on the future of the enterprise networking. Mark discusses the trends we can expect to see in the near future and how the right infrastructure can ensure your enterprise network is secure. - +
Computerworld Live Podcast #96: Security at the Edge 11/06/2008 09:22:22
CW Live speaks with Amol Mitra, HP ProCurve Director of Marketing for Asia Pacific and Japan. Today's topic: how enterprises are starting to shift away from simply controlling security via server logins, firewalls and moving to more adaptive security frameworks. - +
Data Management Edition #10: Multi-Petascale Systems 02/05/2008 09:12:33
This week we look at sustainability and the development of multicore technologies to build multi-petascale systems. - +
IT Security Edition #11: How to poison the Storm botnet 01/05/2008 08:51:55
This week CW Live presents a case study on how to poison the notorious Storm botnet . Plus we take a look at Cisco's plans for Ironport. - +
IT Security Edition #10: Cyber-battles fought and won 24/04/2008 11:09:47
Vendors bow to end user pressure to improve product security, and we take a look at the latest concepts shaping the cyber-battlefield of the future.
Virtual magic: HR specialist throws out 40 servers, adds 8TB SAN and saves $100,000 for disaster recovery 2008-12-01 15:28:00+11
Sybiz adds up for SMEs in downturn 2008-12-01 14:27:00+11
EXCOM scores back-to-back award trifecta 2008-12-01 10:46:00+11
Citect extends SCADA networks with mobility solutions 2008-12-01 09:48:00+11
Citect extends SCADA networks with mobility solutions 2008-12-01 09:48:00+11
Email Archiving 101—Customer Case Study
Join Lee Benjamin, a Microsoft Exchange MVP and Ryan Shipkowski, network administrator for Matthews, to discuss the process and ROI of implementing an email archiving solution, with emphasis on a case study from Matthews International.











