简体   繁体   中英

How to modify PHP FTP upload script to download files instead

I have found a PHP script for transferring FTP files, and it works exactly as I need for one part of my project. The script can upload files via FTP to another server just fine, and can output the progress as it goes.

The code I am using is:

   $fp = fopen($local_file, 'r');
   $conn_id = ftp_connect($source_ftp_server);
   $login_result = ftp_login($conn_id, $source_ftp_user_name, $source_ftp_user_pass);
   $ret = ftp_nb_fput($conn_id, $remote_file, $fp, FTP_BINARY);
   while ($ret == FTP_MOREDATA) {
       // Establish a new connection to FTP server
       if(!isset($conn_id2)) {
           $conn_id2 = ftp_connect($source_ftp_server);
           $login_result2 = ftp_login($conn_id2, $source_ftp_user_name, $source_ftp_user_pass);
       }

       // Retreive size of uploaded file.
       if(isset($conn_id2)) {
           clearstatcache(); // <- this must be included!!
           $remote_file_size = ftp_size($conn_id2, $remote_file);
       }

       // Calculate upload progress
       $local_file_size  = filesize($local_file);
       if (isset($remote_file_size) && $remote_file_size > 0 ){
           $i = ($remote_file_size/$local_file_size)*100;
                   printf("%d%% uploaded<br>", $i);
                   flush();

       }
       $ret = ftp_nb_continue($conn_id);

   }


   if ($ret != FTP_FINISHED) {
       echo "<span style='color:red;'><b>There was an error uploading the file...</b></span><br>";
       exit(1);
   }
    else {
        echo "<br>Files successfully uploaded!<br><br>";    
    }

   fclose($fp);

I took out some unimportant parts, such as extra information that is echoed by the script, etc.

This code works perfectly for uploading files to the other server. However, I also need to download a file from the server using FTP as well.

I'd really like to use the same code as above, with the progress indicator, etc, but am not sure how to modify this code to download a file instead of uploading one.

It may be a couple of simple changes are all that is needed.

Are there any parts of this code in particular that will need to be changed, or can this not work the same for downloads as it does for uploads?

I'd really appreciate it if someone could point me in somewhat of the right direction to sort this out.

Is it as simple as changing the ftp_nb_fput command to a ftp_nb_get command? I don't really understand all of this code so it's difficult to tell what would need to be changed.

Thanks for your help.

You are looking for ftp_get

Looks like it should be used something like the following:

$conn_id = ftp_connect($source_ftp_server);

$login_result = ftp_login($conn_id, $source_ftp_user_name, $source_ftp_user_pass);

$success = ftp_get($conn_id, $local_file, $server_file, FTP_BINARY);

http://php.net/manual/en/function.ftp-get.php

Here's the script, with the necessary modifications to make it download a file instead:

   $fp = fopen($local_file2, 'w+');
   $conn_id = ftp_connect($source_ftp_server);
   $login_result = ftp_login($conn_id, $source_ftp_user_name, $source_ftp_user_pass);
   $ret = ftp_nb_fget($conn_id, $fp, $remote_file2, FTP_BINARY);
   while ($ret == FTP_MOREDATA) {
       // Establish a new connection to FTP server
       if(!isset($conn_id2)) {
           $conn_id2 = ftp_connect($source_ftp_server);
           $login_result2 = ftp_login($conn_id2, $source_ftp_user_name, $source_ftp_user_pass);
       }

       // Retreive size of source file.
       if(isset($conn_id2)) {
           clearstatcache(); // <- this must be included!!
           $remote_file2_size = ftp_size($conn_id2, $remote_file2);
       }

       // Calculate download progress
       $local_file2_size  = filesize($local_file2);
       if (isset($remote_file2_size) && $remote_file2_size > 0 ){
           $i = ($local_file2_size/$remote_file2_size)*100;
                   printf("%d%% downloaded<br>", $i);

       }
       $ret = ftp_nb_continue($conn_id);

   }

   if ($ret != FTP_FINISHED) {
       echo "<span style='color:red;'><b>There was an error downloading the file...</b></span><br>";
       exit(1);
   }
echo "<br>Files successfully downloaded!<br><br>";

   fclose($fp);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM