How to read file from another FTP host without use

2019-03-04 06:49发布

I have a host for store data and a download host (this host doesn't have database). I want to read a file from download host in store host and give it to user for download but I don't want to use monthly bandwidth transfer of store host when user is downloading file and just use download host bandwidth transfer.

There are two ways that I know:

  1. ftp_get download the file and save it in a local file and then set header for download. I don't want use this way because download file in store host.

    // in store host
    $local_file = 'app.apk';
    $ftp_file = '/uploads/2015/06/1eb6a628c60bb69a6b6092d03e252c29.apk';
    // download file and save it in local
    ftp_get($conn_id , $local_file, $ftp_file, FTP_BINARY);
    
    $file_name = 'app.apk';
    $file_size = filesize($local_file);
    
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename=' . $file_name);
    header('Content-Transfer-Encoding: binary');
    header('Connection: Keep-Alive');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . $file_size);
    
    readfile($local_file);
    
  2. I don't know file_get_contents use bandwidth transfer of store host when user is downloading file or not.

    // in store host
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename=' . $file_name);
    header('Content-Transfer-Encoding: binary');
    header('Connection: Keep-Alive');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . $file_size);
    
    // readfile($local_file);
    
    $c = file_get_contents('ftp://login:pass@download-host.com/uploads/2015/06/app.apk');
    echo $c;
    

I don't want to use bandwidth transfer in store host; Which way can I use? Way 2 or another way?

标签: php ftp
1条回答
不美不萌又怎样
2楼-- · 2019-03-04 07:33

There's no way to download a contents from the "download host" directly to the client, without providing the client with all the information needed for the download ("download link").

If you need to hide the download information from the client, you need to download the file on the "store host" and then forward it to the client. Hence you are consuming bandwidth data of the "store host". It does not matter what technology, protocol or function you use. And the ftp_get and file_get_contents("ftp://...") use both the same code behind anyway.

Simply said, there's no way to both hide the download information from the client and not use bandwidth data of the "store host".

查看更多
登录 后发表回答