Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Uploading Files Via FTP

File Handling

Uploading Files Via FTP

by  Glowball  Posted    (Edited  )
This script will upload a file from a client machine that does not have PHP installed to an FTP server with PHP installed. The example specifically uploads a GIF file for testing, but it could be modified to upload any and all file types. If it fails to change directories, you should FTP to your server and see how it lists the directory you want from within the FTP account.

Note that this example is filled with testing feedback text so it is just a starting point. Don't forget to change the permissions on your destination directory so you can write to it (not necessary if your server is Windows-based).

Name the single file "ftp_example.php" and set the variables correctly and it should run for you.
Code:
<?php
if (isset($_POST['upload']) && $_POST['upload'] == 1) {
    // set up the variables
    $ftp_server = "server_name_or_IP_address";
    $ftp_user = "username";
    $ftp_password = "password";
    $destination_directory = "some/directory/from/default/ftp/directory";
    $destination_file = "ftp_upload_test.gif";
    
    // make sure FTP is enabled on the server
    if (function_exists("ftp_connect"))
        echo "FTP enabled on server...<br>\n";
    else {
        echo "FTP is not enabled on $ftp_server!";
        exit;
        }
    
    // set up connection
    $conn_id = ftp_connect($ftp_server);
    
    if (!$conn_id) {
        echo "Could not connect!";
        exit;
        }
    else echo "Connected...<br>\n";
    
    // log in
    $login_result = ftp_login($conn_id, $ftp_user, $ftp_password);
    
    if (!$login_result) {
        echo "Could not log in!";
        exit;
        }
    else echo "Logged in...<br>\n";
    
    // set to passive mode
    $set_passive = ftp_pasv($conn_id, true);
    
    if (!$set_passive) {
        echo "Could not set to passive mode!";
        exit;
        }
    else echo "Connection is set to passive mode...<br>\n";
    
    // set the timeout in seconds
    $set_timeout = ftp_set_option($conn_id, FTP_TIMEOUT_SEC, 300);
    
    if (!$set_timeout) {
        echo "Could not set the timeout!";
        exit;
        }
    else echo "Set timeout...<br>\n";
    
    // change directories
    $change_dir = ftp_chdir($conn_id, $destination_directory);
    
    if (!$change_dir) {
        echo "Failed to change directory!";
        exit;
        }
    else echo "Changed directory...<br>\n";
    
    // upload the file
    $source_path = $_FILES['source_file']['tmp_name'];
    $upload = ftp_put($conn_id, $destination_file, $source_path, FTP_BINARY); 
    
    // check upload status
    if (!$upload)
        echo "FTP upload has failed!";
    else
        echo "Uploaded $source_path to $ftp_server as <b>$destination_file</b>";
    
    // close the FTP stream 
    ftp_close($conn_id); 
    
    echo "\n<hr>\n";
    }
?>

<form action="ftp_example.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="upload" value="1">
<b>GIF File to upload: </b><input type="file" name="source_file"><br>
<input type="submit" value=" Upload ">
</form>
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top