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.

Live Chat Software

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.

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):

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.

15 thoughts on “Tutorial: FTP Upload via cURL

  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. Nice article. Let’s digg it.
    BTW, ‘ and ” in code makes copypasting a little complex.


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

  4. yeah there are a lot roads to Rome


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

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

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

  6. Nice tut. I have worked with Curl very much, but I didn’t know that you could use it with ftp too :)

  7. 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’])) {

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

  9. 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?

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

  11. I never used CURL before this example help me out how i can use CURL very help full :)

  12. Great tutorial! exactly what I was looking for… now to see how to display a status bar.

Comments are closed.