Products
Services
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: (1870)
Favorites:

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

<?php


$ftp_server="10.10.10.1"; //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;
        exit; 
    } 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 
ftp_close($conn_id);
 
?>


See other Scripts in file upload operations

Submit Your Scripts:

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:
*Email:
*Description and Code:
*Enter Code shown
to the right:

[ Refresh Image ]