简体   繁体   English

如何修改PHP FTP上传脚本以下载文件

[英]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. 我找到了一个用于传输FTP文件的PHP脚本,它完全可以满足我的项目一部分的需要。 The script can upload files via FTP to another server just fine, and can output the progress as it goes. 该脚本可以很好地通过FTP将文件上传到另一台服务器,并且可以随时输出进度。

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. 但是,我还需要使用FTP从服务器下载文件。

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? 它像将ftp_nb_fput命令更改为ftp_nb_get命令一样简单吗? 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 您正在寻找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 http://php.net/manual/zh/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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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