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;
}

15 thoughts on “Uploading files to FTP server from PHP Script

  • 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?

    • 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”].

      Rajendra

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

    file1.zip
    file2.zip
    file3.zip
    file4.zip

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

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

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

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

  • 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

Leave a Reply

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