PHP FTP functions tutorial

Before we begin with the tutorial, you may want to download my PHP FTP class. This class is a simple wrapper around the native PHP FTP functions.

Most of the PHP applications store and read their settings from a configuration file. Normally an installer script is used to write the settings to the config file and then an admin section is provided through which the administrator can manage these settings. Normal PHP file functions like fopen and fputs can be used to create and write to the configuration file.

But there is a major drawback with this approach. If you want to create a file/directory through a PHP script in another directory, say, includes, then the directory includes need to be writable by the PHP script. Most of the times this means that you will need to chmod the directory to 777. This is a big security hole.

Another problem is that, more often than not, the owner/group of the newly created file is set to that of the PHP process. PHP process normally run as “nobody” or “www”. This means that the newly created file will be owned by “nobody” or “www” and you might not be able to delete the file through FTP. You will have to delete the file through a PHP script.

A simple workaround to the above problems would be to use the chmod and chown functions. Unfortunately both these functions don’t work on most shared hosting plans.

Therefore we will use PHP’s FTP functions to create files, directories and to change the mode of files. The functions that are of importance to use here are: ftp_mkdir, ftp_put and ftp_site.

Connecting to FTP server

Use ftp_connect function to connect to FTP server and use ftp_login function to login to FTP server.

Creating directory through FTP

Now that we have connected to FTP server, we can create a directory on the server using ftp_mkdir function.

Notice that path specified here is not the filesystem path, its the FTP path. /public_html here is the FTP root and is often different from web server’s document root. Couple of points to keep in mind:

  1. Path specified is not the filesystem path
  2. FTP root is different from web’s server document root

You may specify the directory name only, that is, without the full path. In that case, the new directory will be created where ever you ended up when you logged into the FTP server.

Create file through FTP

Creating a new file through FTP functions is a two step process. First we create a new temporary file using the tmpfile function. Next we upload this temporary file to the server using ftp_fput function.

Change mode of file using ftp_site

We can change the file mode/permissions using the ftp_site function.

PHP5 has built in ftp_chmod, so if you are running PHP5 you can use the ftp_chmod function instead of the above PHP code snippet.

Download the FTP class

I have wrapped up the above functionality into a simple FTP class which you can download here. The class allows you to create directories, files and write to files using PHP’s FTP functions.

I have tested the class on FreeBSD using Apache 2 and PureFTPd and it worked for me. Let me know if you can’t get it to work.

4 thoughts on “PHP FTP functions tutorial

  1. After 3 days testing, learning and searching about the “how to…” work with PHPFTP-Extensions without success I found this page.
    Yahooooo…., thanks for creating a working tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *