Home arrow PHP arrow Page 6 - Building An FTP Client With PHP
PHP

Building An FTP Client With PHP


Ever wondered how to make your own FTP client with PHP? Look no further. In this article Mitchell teaches you everything you need to do just that.

Author Info:
By: Mitchell Harper
Rating: 4 stars4 stars4 stars4 stars4 stars / 71
July 02, 2002
TABLE OF CONTENTS:
  1. · Building An FTP Client With PHP
  2. · PHP and FTP 101
  3. · Retrieving a File Listing
  4. · Retrieving a File
  5. · Building an FTP App
  6. · The GetFile function
  7. · Conclusion

print this article
SEARCH DEVARTICLES

Building An FTP Client With PHP - The GetFile function
(Page 6 of 7 )

GetFile makes use of the ftp_get command to retrieve one selected file from the remote FTP server. After the call to ftp_get, a status message is displayed at the top of the screen indicating whether or not the file was downloaded successfully: a yellow message is shown on success, and a red one on failure.

GetFile starts out by working out where the file should be saved to:

function GetFile()
{
$conn = DoConn();
$currDir = "";

// Get the name of the current directory
if(isset($_GET["currFile"]))
{
$currFile = $_GET["currFile"];

// We need to work out the name of the file
// by stripping away the path, etc

$localFile = strrev($currFile);
$localFile = substr($localFile, 0, strpos($localFile, "/"));
$localFile = strrev($localFile);
$localFile = "___$localFile";


As mentioned earlier, the name of the file is prefixed with three underscores and saved to the root directory on your local machine. The reason I've prefixed the files with three underscores is so that you don't overwrite any important system files by accidentally download files with the same name from the remote server.

GetFile is basically just one call to ftp_get with some error handling:

if(@ftp_get($conn, $localFile, $currFile, FTP_BINARY))
{
// Success
}
else
{
// Failure
}


If the file was retrieved OK, then a message like the following is shown at the top of the screen:

A successful file retrieval

On the other hand, if something goes wrong then a message like the one shown below is displayed:

A failed file retrieval

Now, at this point we've taken a look at both the ShowFiles and GetFile functions. If you've never worked with PHP and FTP before, then let me set this challenge for you:

I'm not going to provide you with the code for the PutFile, RenameFile or DeleteFile functions. Instead, head on over to the PHP FTP reference page and see if you can workout how to accomplish these functions using PHP's built-in FTP functionality. It's actually quite easy to do, and by doing so you will help yourself to become a better coder.
blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials