Uploading files to FTP server from PHP Script

For one of my project I was required to upload files on an FTP server from the PHP script. Searching on Google I found code required but it was not completely available at one place. So I thought to share this here for use by others as tried and tested code, working on a live website.

This script was created for moving the just uploaded file to FTP server. That is why it uses $_FILES[“tmp_name”] as local file. You can change to your file location, if it is already uploaded.


Please replace the variables in first four lines according to the settings you have for your target FTP server.

$ftp_server = "ftp.yourserver.com";
$ftp_user_name = "ftpuser";
$ftp_user_pass = "ftppassword";
$remote_dir = "/target/folder/on/ftp/server";

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = @ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

//default values
$file_url = "";

if($login_result) {
//set passive mode enabled
ftp_pasv($conn_id, true);

//check if directory exists and if not then create it
if(!@ftp_chdir($conn_id, $remote_dir)) {
//create diectory
ftp_mkdir($conn_id, $remote_dir);
//change directory
ftp_chdir($conn_id, $remote_dir);

$file = $_FILES["file"]["tmp_name"];
$remote_file = $_FILES["file"]["name"];

$ret = ftp_nb_put($conn_id, $remote_file, $file, FTP_BINARY, FTP_AUTORESUME);
while(FTP_MOREDATA == $ret) {
$ret = ftp_nb_continue($conn_id);

if($ret == FTP_FINISHED) {
echo "File '" . $remote_file . "' uploaded successfully.";
} else {
echo "Failed uploading file '" . $remote_file . "'.";
} else {
echo "Cannot connect to FTP server at " . $ftp_server;


  1. RosenoJuly 6, 2013

    I’m looking for a code in php ftp to overwrite a file via upload. tested several codes but only yours could connect with my server via ftp. how can I adapt your code into a form to replace my file?

    1. RajendraJuly 7, 2013

      Hi Roseno,

      Good to know that my code helped you in solving your issue. To move a file using FTP for file uploaded through a form you just need to replace the field name used to select files in above code – in place of $_FILES[“file”][“tmp_name”] use $_FILES[““][“tmp_name”].


  2. MikeJuly 15, 2013

    Great example. Thanks so much…. How could this code be used to upload multiple files that were already on the server.

    Lets say I have 4 files in a subfolder relative to the php file that does the upload, lets call the subfolder “/fileshere/” withe the following 4 files in it:


    I need the script to upload each of the files, then give a done message. Could you provide some code to do that.

  3. JamesAugust 17, 2013

    I am trying to figure out how to fix the following WP error when I try to upload a theme or plugin:
    Failed to connect to FTP Server localhost:21
    Any link I can learn from, thank you.

    1. RajendraAugust 17, 2013

      This is due to the permission issues. WordPress asks for FTP user name and password when the temp directory it uses for uploading the plugins or themes is not writeable by the PHP script. Then you need to input the FTP access in the wordpress backend. From your message it seems that you are providing localhost as host name. You need to provide the IP of the server or full domain name without http like example.com. The port number is correct for FTP access. Make sure your user name and password is correct and does not have spaces in the beginning and at the end.

  4. ARTHISeptember 1, 2013

    Hi im junior web developer in singapore..Im trying multiple file uploading via http://ftp..but it not works for me..can you provide this codeing with HTML form…

  5. monsterjeanJanuary 29, 2014

    the code creates the directory, so I know it is connecting, but the file I attempt to upload fails. Can you help? This is the (almost) functioning upload page in action: http://monsterjean.com/PDP/testftpupload.php

  6. RoyJune 17, 2014

    We have a similar script working but we are uploading large files to our FTP, Since that the page does not show progress the page is “dead” is it possible to build in a progress bar (propably not with PHP)

    1. RajendraJuly 22, 2014

      You can try to create a progress bar using the third party tutorials. While the file is being uploaded via FTP. You can execute another ajax request at regular intervals just to check the file size uploaded. And show the value returned in the Progress bar using HTML/CSS/JS. here is a link if it helps: http://www.sitepoint.com/tracking-upload-progress-with-php-and-javascript/. You can find many on Google.

  7. SreeApril 17, 2017

    We are doing a project on High Availability file storage system. Your code helped us a lot.
    Thank you

    1. RajendraApril 17, 2017

      Good to know that it helped you.

  8. KamaljeetApril 26, 2017

    Just amazing. Read atleast 50 articles before this and nothing was working 🙂

    Thanks Man!

    1. RajendraApril 26, 2017

      Thank you Kamaljeet. Good to know that it helped you.

  9. FirstJeannaNovember 10, 2017

    I see you don’t monetize your blog, don’t waste your traffic, you can earn extra cash every month because you’ve got hi quality
    content. If you want to know how to make extra $$$, search for:
    Boorfe’s tips best adsense alternative

  10. WeshJuly 4, 2019

    Thank you for this. Worked perfect!

    1. RajendraJuly 4, 2019

      Thank you. I am glad that it is still helping someone.


Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to top