1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

PHP Ftp Multiple Files?

Discussion in 'PHP & Perl' started by rsquare, Oct 12, 2008.

  1. rsquare

    rsquare Newbie

    Joined:
    Jul 16, 2008
    Messages:
    30
    Likes Received:
    38
    I use the following PHP script fragment to DL one file to my server and it works great. But I would like to DL multiple files from the same server. I would like to be able to call to a txt file with a list of the files and then download each in turn to the server. Any help, suggestions, or sample code would be appreciated.

    Code:
    $ftp_server = "server.com";
    $ftp_user_name = "name";
    $ftp_user_pass = "password";
    $source_file = "/dir/file1.zip";
    $destination_file = "/home/public_html/dl/file1.zip";
     
    $conn_id = ftp_connect($ftp_server);
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
     
    if ((!$conn_id) || (!$login_result)) {
         echo "FTP connection has failed to $ftp_server as $ftp_user_name.<br />";
         exit;
    } else {
         echo "Connected to $ftp_server successfully as $ftp_user_name.<br />";
    }
     
    $download = ftp_get($conn_id, $destination_file, $source_file, FTP_BINARY);
     
    if (!$download) {
         echo "FTP download has failed!";
    } else {
         echo "Downloaded $source_file from $ftp_server as $destination_file";
    }
    fclose($fp);
     
  2. chc89

    chc89 Junior Member

    Joined:
    Jan 18, 2008
    Messages:
    118
    Likes Received:
    3
    You can use foreach loop to upload or download multiple files from your server or to your server. You Can use same code with the loop.

    Hope it helps
     
  3. seo101

    seo101 Registered Member

    Joined:
    May 2, 2007
    Messages:
    94
    Likes Received:
    15
    yup exactly just use the loop.. here's an example :
    PHP:
    //asume you put each filename per line on list-file.txt
    $f fopen("list-file.txt""r");
    while ( 
    $lines fgets($f1024)) {
        
    $filename $lines;
                 
    $destination_file "/home/public_html/dl/$filename";
                 
    myftpfunction($destination_file);
    }
    fclose($f);


    function 
    myftpfunction($destination_file){
    //your ftp code goes here
    }
     
  4. rsquare

    rsquare Newbie

    Joined:
    Jul 16, 2008
    Messages:
    30
    Likes Received:
    38
    I am following you, but how do I reference the $source_file for the multiple files that I want to DL from the server?
     
  5. bwh48

    bwh48 BANNED BANNED

    Joined:
    Jun 30, 2007
    Messages:
    56
    Likes Received:
    54
    Check out

    http://aziz.oraij.com/

    He has a great class for doing this sort of stuff..
     
  6. bwh48

    bwh48 BANNED BANNED

    Joined:
    Jun 30, 2007
    Messages:
    56
    Likes Received:
    54
    Check out

    http://aziz.oraij.com/

    He has a great php class for doing bulk FTPing in PHP