PHP FTP Script - Help Needed!

Discussion in 'Programming' started by FFX, May 23, 2009.

  1. #1
    I am trying to setup a PHP script that can upload a zip file from a directory to a variable directory. What I am trying to do is creating an "custom auto install script" except it dont run the install.

    So heres what I need.

    I need a form where users post their ftp account, username and type of file to be uploaded and unziped to.

    Example:

    What The User Types In:
    FTP Username: ftpuser
    FTP Password: ftppass
    File: file1.zip, file2.zip

    What the PHP Script do:
    Transfer from: http://www.files.mydomain.com
    to: http://www.user.mydomain.com
    Unzip's file

    What I currently have

    <?php
    $conn = ftp_connect("ftp.files.mydomain.com") or die("Could not connect");
    
    ftp_login($conn,"username","password");
    
    ftp_nb_get($conn,"file1.zip","file1.zip",FTP_BINARY);
    
    $conn = ftp_connect("ftp.user.mydomain.com") or die("Could not connect");
    
    ftp_login($conn,"username","password");
    
    ftp_nb_put($conn,"file1.zip","file1.zip",FTP_BINARY);
    
    ftp_close($conn);
    ?>
    Code (markup):
    This did not work tough :p I tried it and the file dissapeared :eek:

    In order to make things variable shouldnt I have

    <?php
    $conn = ftp_connect("ftp.files.mydomain.com") or die("Could not connect");
    
    ftp_login($conn,"[COLOR="Red"][[Username]][/COLOR]","[COLOR="Red"][[Password]][/COLOR]");
    
    Code (markup):
    To make it variable? And then create a PHP Post script?
     
    FFX, May 23, 2009 IP
  2. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I noticed you did not specify a path at all when you log into either FTP servers, and you do not do any kind of error checking to see what the response was for trying to retrieve the file, nor if you have a file to push up.

    So set your paths once you login, since your file may be at /var/www/username/ , but logging in might put you at /var/www/username/public_html/ initially, and trying to download file1.zip obviously would error out if its in the path above your initial login location.


    If you want to make the username and password a variable you can do it in this way

    $usr = "myusername";
    $pwd = "mypassword";

    then
    ftp_login($conn,$usr,$pwd);

    No sense in putting quotes around a variable unless you're trying to somehow merge the value of it into another string.
     
    kblessinggr, May 24, 2009 IP
  3. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Also take a look at the example provided by php.net itself.

    
    <?php
    
    // Initate the download
    $ret = ftp_nb_get($my_connection, "test", "README", FTP_BINARY);
    while ($ret == FTP_MOREDATA) {
       
       // Do whatever you want
       echo ".";
    
       // Continue downloading...
       $ret = ftp_nb_continue($my_connection);
    }
    if ($ret != FTP_FINISHED) {
       echo "There was an error downloading the file...";
       exit(1);
    }
    ?>
    
    PHP:
    As you can see the download doesn't simply happen just cuz you called it. You have to wait til its finished.

    your method requires ftp_get() not ftp_nb_get()
     
    kblessinggr, May 24, 2009 IP