Uploading files using PHP's FTP method

Discussion in 'PHP' started by Weirfire, Dec 14, 2005.

  1. #1
    Has anyone used PHP's method ftp_put?

    Because of the type of server I can't do the upload with copy() or move_uploaded_files therefor the ftp_put method seems to be the only 1.

    Below is the code I have so far;

    // variables
    $ftp_server = "<IP>";
    $ftp_user_name = "<username>";
    $ftp_user_pass = "<password>";
    $destination_file = "images/catalogue/".$_FILES['image']['name'];
    $sourcefile = $_FILES['image']['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 "FTP connection has failed!";
           echo "Attempted to connect to $ftp_server for user $ftp_user_name"; 
           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);  // line 30
    
    // 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); 
    
    Code (markup):
    And I've been getting the following output;

    I have commented which line is line 30. If anyone can see what the problem is then please let me know. File uploads have been one of my greatest evils this year.
     
    Weirfire, Dec 14, 2005 IP
  2. PinoyIto

    PinoyIto Notable Member

    Messages:
    5,863
    Likes Received:
    170
    Best Answers:
    0
    Trophy Points:
    260
    #2
    Well I am not sure really what is the purpose of this but I use this code to upload images or other file in my site and it works perfectly.

     
    PinoyIto, Dec 14, 2005 IP
    Weirfire likes this.
  3. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #3
    Thanks edpudol but above I mentioned I couldnt use that function. It's a real pain to be honest.

    I appreciate your help though :)
     
    Weirfire, Dec 14, 2005 IP
  4. cornelius

    cornelius Peon

    Messages:
    206
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #4
    i use PEAR http_upload

    its a very handy library worth a look
     
    cornelius, Dec 15, 2005 IP
  5. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #5
    Does it use the ftp put method? If not then it will be using the copy() or move_uploaded_files() method which doesn't work on the server I'm using.

    Anyone use ftp put? Anyone at all? lol
     
    Weirfire, Dec 15, 2005 IP
  6. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #6
    So no-one has used the ftp put method? I am the only one stupid enough to try it? lol
     
    Weirfire, Dec 15, 2005 IP
  7. cornelius

    cornelius Peon

    Messages:
    206
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #7
    no never tried it, as i said the above PEAR library does the job, try it first.

    is it a windows server?
    u on shared server?
     
    cornelius, Dec 15, 2005 IP
  8. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #8
    I've tried that method already cornelius :)

    I'm pretty sure it is indeed a Windows shared server.
     
    Weirfire, Dec 16, 2005 IP
  9. ravianz

    ravianz Notable Member

    Messages:
    1,536
    Likes Received:
    55
    Best Answers:
    0
    Trophy Points:
    250
    #9
    never tried the ftp upload function coz PEAR lib meets the requirement
     
    ravianz, Dec 17, 2005 IP
  10. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #10
    thanks for letting me know ravianz lol
     
    Weirfire, Dec 17, 2005 IP
  11. cornelius

    cornelius Peon

    Messages:
    206
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #11
    to be honest im out of ideas, i only have/use linux servers (mainly due to cost and i use php not asp.net),

    try http://uk.php.net/ftp theres a good few replies and sample code,

    also u might try a php proggie called EzUpload its good for uploading files
     
    cornelius, Dec 17, 2005 IP
  12. godhand_ez

    godhand_ez Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    i think i've found the problem, you defined your variable as $sourcefile and on line 30 you use it as $source_file that's why the outpost reads the variable is undefined
     
    godhand_ez, Jan 25, 2006 IP
  13. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #13
    Thanks for having a look at the code godhand_ez :)

    It's certainly a great way to start posting at this forum as well! Weclome to DP mate. I hope it's not your last time here! :)
     
    Weirfire, Jan 26, 2006 IP
  14. Nanofied

    Nanofied Guest

    Messages:
    1,028
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Im going to play with this later... :)
     
    Nanofied, Jan 31, 2006 IP
  15. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #15
    It's really great of you to let us all know that! :p
     
    Weirfire, Feb 1, 2006 IP