Uploading file to another server from PHP form

Discussion in 'PHP' started by KingCobra, Jul 30, 2009.

  1. #1
    KingCobra, Jul 30, 2009 IP
  2. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #2
    You can make a ftp connection with php.

    
    <?php
    // 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); 
    
    // 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); 
    ?>
    
    PHP:
     
    Kaizoku, Jul 30, 2009 IP
  3. KingCobra

    KingCobra Well-Known Member

    Messages:
    289
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #3
    Kaizoku,

    Your code works fine. thanks.

    I need little bit more with this script. I want that if the specified dir/folder doesn't exists then the script will create an dir/folder (that is specified) and upload file there. How can I check and make dir?

    Please help.
     
    KingCobra, Jul 31, 2009 IP
  4. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #4
    Kaizoku, Jul 31, 2009 IP
  5. jamespv85

    jamespv85 Peon

    Messages:
    238
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5

    ftp_chdir should return a true or false if it can or can't change to that directory, which should then give you an idea if the dir already exists.

    but this is not always true. there might be other reasons why it can't change to a specified directory aside from it not existing yet, so you should have something to watch on the errors returned by the function.
     
    jamespv85, Aug 1, 2009 IP
  6. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #6
    You can always use ftp_nlist to return array of files and folders, then just cross reference if it exists or not.

    Or

    You can just do ftp_mkdir, it will only create the directory if it doesn't exist... (I think)
     
    Kaizoku, Aug 1, 2009 IP
  7. KingCobra

    KingCobra Well-Known Member

    Messages:
    289
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #7
    I want to create a text (.txt) file in ftp dir and write something inside that file by php.
    How can I do that?
     
    KingCobra, Aug 1, 2009 IP
  8. codebreaker

    codebreaker Well-Known Member

    Messages:
    281
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    110
    #8
    
    <?php
    $fp = fopen('data.txt', 'w');
    fwrite($fp, '1');
    fwrite($fp, '23');
    fclose($fp);
    
    // the content of 'data.txt' is now 123 and not 23!
    ?>
    
    Code (markup):
    From the php manual.
    You can google for more :).
     
    codebreaker, Aug 1, 2009 IP
  9. wildogre

    wildogre Well-Known Member

    Messages:
    1,018
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    128
    #9
    Thanks a lot codebreaker for sharing knowledge.
     
    wildogre, Aug 1, 2009 IP