Skip to main content

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.

//Make the file writable by all

//Open the file and write to it
$fp = fopen('somefile', 'w');
fputs($fp,$data); fclose($fp);

//Make the file writable only by the owner

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.

//Connect to the FTP server
$ftpstream = @ftp_connect('localhost');

//Login to the FTP server
$login = @ftp_login($ftpstream, 'user', 'secret');
if($login) {
	//We are now connected to FTP server.

//Close FTP connection

Creating directory through FTP

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

//Create a directory 'config' at the root of your website
@ftp_mkdir($ftpstream, '/public_html/config');

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.

//Create a temporary file
$temp = tmpfile();

//Upload the temporary file to server
@ftp_fput($ftpstream, '/public_html/config/', $temp, FTP_ASCII);

Change mode of file using ftp_site

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

//Make the file writable by all
ftp_site($ftpstream,"CHMOD 0777 /public_html/config/");

//Write to file
$fp = fopen('/home/jatinder/public_html/config/', 'w');
fputs($fp,'Learnt this at');

//Make the file writable only to owner
ftp_site($ftpstream,"CHMOD 0644 /public_html/config/");

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 to “PHP FTP functions tutorial”

  1. Great tutorial! One question: where in the script would you put a setting for Passive FTP mode? Thanks in advance.

  2. Your FTP tutorial was a great help to me, i needed a workaround for the php safe mode and you snippets where a great help….thanks

  3. 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.

Comments are closed.