Secure File Copy using FTP through HTTP

By Hawkee on Mar 13, 2005

I've found this to be quite useful in handing uploaded files through HTTP. You can use this to move the file from your /tmp folder to a folder on your site that isn't mode 777. The concept is simple. It connects to your FTP server and puts the file in the folder of your choice. Usage:

ftp_copy("/tmp/tmpfile", "public_html/uploads");

<?php

function ftp_copy($source_file, $destination_file)
{
    $ftp_server = 'ftp.server.com';
    $ftp_user = 'login';
    $ftp_password = 'password';

    $conn_id = ftp_connect($ftp_server);
    $login_result = ftp_login($conn_id, $ftp_user, $ftp_password);

    if((!$conn_id) || (!$login_result))
    {
            echo "FTP connection has failed!";
            echo "Attempted to connect to $ftp_server for user $ftp_user";
    }

    $upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
    ftp_close($conn_id); 

    if(!$upload)
    {
           echo "FTP copy has failed!";
       return false;
    }
    else
    {
        return true;
    }   
}

?>

Comments

Sign in to comment.
Gex   -  Apr 25, 2007

to be more clear...the code should be >

if((!$conn_id) || (!$login_result))
   {
            echo \"FTP connection has failed!\";
            echo \"Attempted to connect to $ftp_server for user $ftp_user\";
exit();
      }
 Respond  
Gex   -  Apr 25, 2007
if((!$conn_id) || (!$login_result))
    {
            echo \"FTP connection has failed!\";
            echo \"Attempted to connect to $ftp_server for user $ftp_user\";
    }

this block of code should be followed by exit(); to halt the rest of code...if am write..the rest of code will be executed if the connection to ftp failed

 Respond  
log2   -  Mar 13, 2005

Not bad... not bad at all, infact I could use this... but with my ftp... i don\'t think that would work too well lol, but i\'ll try, good job Hawkee

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.