douhe8981 2015-06-04 04:24
浏览 31
已采纳

如何在不使用主机带宽传输的情况下从其他FTP主机读取文件

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?

  • 写回答

1条回答 默认 最新

  • dqq46733 2015-06-04 05:27
    关注

    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".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启