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 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.
Related posts
- Uploading files with cURL?
- Create custom backups from your website using cURL
- Curl: Location redirect while open_basedir is set
Comments
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)
Nice article. Let’s digg it.
BTW, ‘ and ’ in code makes copypasting a little complex.
Sincerely,
Alex
If you don’t have (or don’t want) libcurl, try:
http://php.net/manual/en/wrappers.ftp.php
isset($_POST[‘Submit’])
It’s not a good idea to rely on the Submit button being clicked.
isset($_FILES[‘upload’]) would be better.
[...] guía de como implementar Upload de archivos por FTP utilizando PHP. Para ello nos hemos guiado de FTP Upload via cURL en donde esta muy bien [...]
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.
[...] guía de como implementar Upload de archivos por FTP utilizando PHP. Para ello nos hemos guiado de FTP Upload via cURL en donde esta muy bien [...]
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.
Sorry, the comment form is closed at this time.










FTP Upload via cURL…
Uploading files to a remote FTP server is very effective while using the cURL library. This short PHP tutorial will show the user how to upload a file to a password protected FTP server….