Contact Us

Scripts: Php :: File upload operations >> Library Article #2

Developer's Section

Upload a file using an FTP connection
By: Erobo Team Member

Hire a Developer for Related Work / Installation | $55 hr
Rating:  | Rate It:   
Average Votes: (2749)

Learn to upload a file via FTP by using a ftp connection in PHP.

This script first stablishes an FTP connection to the server. Later it uses ftp_put() method to upload the local file to a folder in your server.

Code to upload a file via FTP:

 Code Snippet 1


$ftp_server=""; //your server address here
$ftp_user_name = "Your server's username";
$ftp_user_pass = "Your server's password";
$source_file = $_FILES["uplaodedfile"]["tmp_name"];
$destination_file =  "uploads/";   
$destination_file .= basename( $_FILES['uploadedfile']['name']);

// 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); 

// check connection
if ((!$conn_id) || (!$login_result)) { 
        echo "Unable to connect to server:" . $ftp_server;
    } else {
        echo "Connected to $ftp_server, for user $ftp_user_name";

// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY); 

// check upload status
if (!$upload) { 
        echo "FTP upload has failed!";
    } else {
        echo "Uploaded $source_file to $ftp_server as $destination_file";

// close the FTP stream 

See other Scripts in File upload operations

Submit Your Scripts:

System Message:
  • Your PHP script has been sent. Please allow 48 hours for processing.

If you would like to have your PHP scripts published in this section please fill out
the form below:
*Your Name or Username:
Home Town:
*Description and Code:
*Enter Code shown
to the right:

[ Refresh Image ]