Tutorial: FTP Upload via cURL

FTP hosting is often much cheaper than regular web hosting. The upload with an ftp client is for sure the most common way, but could be a problem for people behind a firewall or without enough rights (capabilities) to install a FTP client. For those a upload via a web form is the best solution.

Upload limitations by your web server

The default value for file uploads within PHP is 2MB, if you need to upload bigger files you need to change your PHP configuration or need to create a .htaccess file with this code to upload files of max. 16MB:

php_value upload_max_filesize 16M
php_value post_max_size 20M

The value for the post_max_size is larger than the value for the upload_max_size because we want to be able to upload more than just a file (also other data via text fields or text areas). The .htaccess file needs to be in the same directory than your PHP upload script.

Using cURL for file transmissions

cURL is a great library for file transmissions via different types of protocols. The library supports the transport via POST, GET, FTP upload and much more. cURL is also able to authenticate on the target server or website.

In this tutorial we want to upload a file to some (password protected) remote FTP server via a web form.

<form action="curlupload.php" method="post" enctype="multipart/form-data">
<div>
<label for="upload">Select file</label>
<input name="upload" type="file" />
<input type="submit" name="Submit" value="Upload" />
</div>
</form>

The form is simple and has only one file field and the submit button. Don’t forget to protect this kind of pages.

Next we need some PHP code to handle the upload and opens a stream to send the file via cURL to the remote FTP server (place this code above the html code):

if (isset($_POST['Submit'])) {
 if (!empty($_FILES['upload']['name'])) {
 	$ch = curl_init();
 	$localfile = $_FILES['upload']['tmp_name'];
 	$fp = fopen($localfile, 'r');
 	curl_setopt($ch, CURLOPT_URL, 'ftp://ftp_login:password@ftp.domain.com/'.$_FILES['upload']['name']);
 	curl_setopt($ch, CURLOPT_UPLOAD, 1);
 	curl_setopt($ch, CURLOPT_INFILE, $fp);
 	curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
 	curl_exec ($ch);
 	$error_no = curl_errno($ch);
 	curl_close ($ch);
        if ($error_no == 0) {
        	$error = 'File uploaded succesfully.';
        } else {
        	$error = 'File upload error.';
        }
 } else {
    	$error = 'Please select a file.';
 }
}

After the user has selected a file, the data is uploaded to the web server. We open the temp file with fopen and the cURL session is initialized. Together with the URL for the remote FTP server, we send the FTP login and password to the target location. The other cURL settings are required to send the file via the FTP protocol to the target server. If the returned error code is equal “0”, the upload was successful.

This small PHP snippet is responsible for the upload to some remote FTP server. We suggest using more validation routines in a production environment.

Published in: PHP Scripts

15 Comments

  1. Nick, you can use cURL with a lot of protocols:

    http, https, ftp, gopher, telnet, dict, file and ldap (got that from the php manual) ;)

  2. Thanks Alex,

    the problem with the single quotes is a problem in WordPress :(

    While the PHP code is parsed with Geshi, WP is parsing the code again.
    Try to color parse HTML code is a nightmare on WordPress Blogs

  3. isset($_POST[‘Submit’])

    It’s not a good idea to rely on the Submit button being clicked.

    isset($_FILES[‘upload’]) would be better.

  4. Hi Dean,

    this first test for the submit is from a standard template I use, but there is also a test for an empty file field:

    if (!empty($_FILES[‘upload’][‘name’])) {

    this code would start transmitting an empty file array too ;)
    if (isset($_FILES[‘upload’])) {

  5. This one nice tutorial. I absolutely love the CURL library. However, there are FTP function in PHP which you can use to do the same thing and probably work faster and is more efficients.

  6. However, there are FTP function in PHP which you can use to do the same thing and probably work faster and is more efficients.

    I don’t agree, the upload via a browser without any lib, is maybe not possible on many shared hosting configurations.

    Check how simple and small this snippet is.

    Maybe you can provide a more simple / faster example?

  7. As an alternative to using cURL for FTP, there’s always the native FTP_* functions for PHP which do quite a good job. Although I have to admit that although cURL is an advanced topic, it’s very powerful and I would encourage everybody to utilise it.

Comments are closed.