Home arrow PHP arrow Page 2 - 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 - PHP and FTP 101
(Page 2 of 7 )

PHP has 25 functions that allow us to work with FTP servers. Transferring files with FTP is the most common way to copy/move files from one server to another. It's also one of the quickest and easiest methods.

As with any protocol, a connection to the remote server is required before commands can be sent to it. With PHP, we use the ftp_connect function. Its signature is shown below:

resource ftp_connect ( string host [, int port [, int timeout]])

Ftp_connect accept one compulsory and two optional parameters:
  • host: The IP address, domain name or NetBIOS name of the remote host to which we are requesting the FTP connection.
  • port: An optional parameter which can be used to specify the port to attempt to connect to. If this is left empty, then the default FTP port of 21 is used.
  • timeout: An optional parameter which can be used to specify the number of seconds to waiting before a connection is deemed to have “timed out”. If not specified, the default value of 90 seconds will be used.
Ftp_connect returns a resource variable which holds the details of the newly established FTP connection. This variable then needs to be passed to the ftp_login function along with a username and password to authenticate ourselves against the remote FTP server.

The ftp_login functions signature looks like this:

bool ftp_login ( resource ftp_stream, string username, string password)
Putting the ftp_connect and ftp_login functions together, we can connect to and authenticate with a remote FTP server with the following PHP code:

<?php

$conn = @ftp_connect("my.ftpserver.com")
or die("Couldn't connect to FTP server");

$login = @ftp_login($conn, "username", "password")
or die("Login credentials were rejected");

?>


Notice how I've used the @ symbol prepended to the calls to ftp_connect and ftp_login? This stops any errors from being output to the client, and instead we use the die function to terminate with an error message if either ftp_connect or ftp_login fail.

Once connected to the FTP server, it's common to retrieve the name of the current directory that we are working with and also to display the files and directories which that directory contains. The ftp_pwd function returns the current directory name, and its signature looks like this:

string ftp_pwd ( resource ftp_stream)

As you can probably guess, we need to pass the connection resource created from the call to ftp_connect to ftp_pwd. Building on our last example, we can connect to the Microsoft FTP site and show the current working directory like this:

<?php

$ftpServer = "ftp.microsoft.com";
$ftpUser = "anonymous";
$ftpPass = "me@myhost.com";


set_time_limit(160);

$conn = @ftp_connect($ftpServer)
or die("Couldn't connect to FTP server");

$login = @ftp_login($conn, $ftpUser, $ftpPass)
or die("Login credentials were rejected");

$workingDir = ftp_pwd($conn);
echo "You are in the directory: $workingDir";


ftp_quit($conn);

?>


The highlighted code is new. I've defined the FTP server, user and password as variables and I've also used set_time_limit to extend the time that PHP should try to connect from 90 seconds to 160 seconds.

As the code above shows, we use ftp_pwd to get the current working directory on the FTP server as the $workingDir variable. We then echo this to the screen, and call ftp_quit to close the FTP connection.

The output from the script is simple and looks like this:

You are in the directory: /

Now that we understand the basics of connecting to, logging into, and retrieving the current working directory from a remote FTP server, let's look at how to retrieve files, how to change directories, etc.
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