Note: This is a basic tutorial meaning we are using all predefined function libraries and no extended classes with exception handling. It doesn't offer the complete in-depth analysis you should acquire before attempting to build a stand alone PHP based FTP system. However, it will allow you to create a basic interaction system for rare occassions.
In this tutorial we will look at the built in FTP functions that PHP versions 4 and 5 offer us. Note that most of these functions can be found in PHP versions 3 3.0.13 and above except for ftp_close() which requires version 4.2.0.
In this tutorial we will:
- Connect to an FTP server
- Login to an FTP server
- Retrieve a single file
- Upload a single file
- Retrieve a list of files in a directory
Let's start off by connecting to the server. You should have your FTP host setup in the function ftp_connect() like so:
$resource = ftp_connect('ftp.host.com');
This will establish a connection to the host and let you know if it even exists. If that host cannot be found, an error will be returned.
The connection and login process is very simple and self explanitory. The login function is ftp_login() and is used like so:
$login = ftp_login($resource, 'username', 'password');
You'll notice that we have to call the original FTP resource locator. You should become comfortable with doing this, since most if not all FTP functions require an FTP resource to be executed.
If you have an FTP server setup and you know your connection info you shouldn't have gotten any errors at this point. Now it's time to get into the useful features of PHP's FTP interaction system. First, let's retrieve a list of files from a specific directory.
$list = ftp_rawlist($resource, '/mydir');
This will return a raw list of all the files in a directory. Depending on the operating system, a series of information could be returned for each file name. If you want to check the OS of the FTP server you can use the ftp_systype() function; however, since this is a basic tutorial we are not going to worry about that and we're going to continue listing files without specific interpretation.
However, for our exampe we're going to assume we're using a UNIX operating system. For each file you should receive a bunch of code similar to this piece:
rw-r--r-- 1 32009 user_account 6435 Feb 19 20:19 filename.ext
There are three important aspects of that returned code. The first being the type of the "file" being returned, the second the modified date, and the last the actual file name.
If you notice the first file name prefix, rw-r--r-r--, you can probably guess it has special meaning. Well, it does. For a little information about what these things mean you can check out http://nik.seet.com/ftp.html. However, this doesn't offer a very comprehensive insight so you may need to do more research if you want to get into directory/file distinguishing.
Next is the modified date, it's pretty self explanitory and I think you all can figure out which piece of code that is. Lastly is the file name, this is useful because, of course, it's the actual file name on the server.
If you're becoming comfortable with php you might want to try an explode() or preg_split() on the file information to make it cleaner to read.
Now that we have a list of files it's time to retrieve them and see their contents. For this we'll use the ftp_get() function. Let's look at an example:
$file = ftp_get($resource, 'tmp.txt', '/dir/myfile.ext', FTP_BINARY);
You'll notice we called the FTP resource stream stored in $resource again. Next is the file on the local server where the file contents should be stored. A simple .txt will do in most cases; however, you should chmod it accordingly so the entire world can't see the contents even if you're erasing them after the script is run. The next argument is the remote file path, this should be the path to the file on the FTP server that you would like to retrieve. The final argument is the transfer mode. For the transfer mode we're going to use a standard FTP_BINARY mode rather than the FTP_ASCII transfer mode. These are the only two transfer modes in the php function library so you may want to read more on the uses of each.
To view the contents of the file transferred from FTP you'd have to echo the file contents of the local file you specified in the ftp_get() function.
Next we're going to put a file on the FTP server. It's very similar to the ftp_get() function and it's called ftp_put(). Let's check it out:
$putFile = ftp_put($resource, '/dir/myuploadedfile.ext', 'filetoupload.ext', FTP_BINARY);
Again we use the resource as the first argument. The second argument is the path on the FTP server in which the file should be uploaded. Thirdly is the local file on the PHP server that is being uploaded. The last required argument is the transfer mode again. Read above for a bit more information.
When you're all said and done with the ftp interaction in your script you should make a call to ftp_close() if you're running a PHP version of 4.2.0 or higher. In most cases, all hosts have this or a later version installed already.
The ftp_close() function takes one argument, the FTP resource. We'd call it like this:
Okay, that's it! If you digested this perfectly then you could be ready to start interacting with FTP servers via PHP. I will create a more in-depth tutorial later next week about building classes and using OOP to help make the experience more customized and easier to navigate.
While FTP isn't nearly as fast as retrieving file contents from a local server, it can offer much more functionality in interacting with servers besides your own.
- Caffeine Lounge
- Corner Cubicle
- Student Campus
- Software Development
- Industry News
- Introduce Yourself
- C and C++
- .NET Framework
- Other Languages
- Game Development
- Mobile Development
- 52 Weeks Of Code
- Web Development
- HTML & CSS
- Graphic Design
- Flash & ActionScript
- SEO & Advertising
- Web Servers & Hosting
- Site Check
Follow & Share
Give It A Try!
- Display Formatted Code
- Simple Content Formatting with Regular Expressions
- Limiting login attempts
- CodeIgniter Text Based Mafia Game || Login/Usersonline part 3
- Adding Your Own Functionality To Smarty w/ Plugins
- CodeIgniter Text Based Mafia Game || Register part 2
- CodeIgniter Text Based Mafia Game || Intro / Set-up Part 1
- Functions that return booleans for fun and profit.
- Making a webmail system | PHP | Part 2
- Wacky logic with looping and lambdas
- 168 More PHP Tutorials...
- PHP left() and right()
- IP Logger
- Full url of the page
- Login using Sessions in PHP
- online file editor
- Years, Days, Hours, Minutes Ago
- Get the Title of a URL
- Fibonacci series
- Checkbox list storing
- Page Generation
- 342 More PHP Snippets...