dozpv84422 2016-12-02 05:20
浏览 186
已采纳

使用Readfile从FTP下载文件

I want to give my users the option to download files from their unique directory on a remote FTP server, however some of these are incredibly large in size. I have tried using FileSaver.js but this struggles with large files as I have to read them from the remote server to the local web-server first, then save them to the client. The alternative StreamSaver.js is a better option as it supports chunking, however it has little browser support.

Using the readfile in PHP works perfectly, where it streams the download to the client as it reads the data into memory. The problem is that the URL its pulling from contains FTP credentials, thus I don't want to link directly to it.

Is there any option that would allow me to provide them with some type of a "Download" button and still make use of the following code?

$file_url = 'ftp://username:password@124.23.148.103/124.23.148.103 port 25665/server.jar';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary"); 
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\""); 
readfile($file_url);
  • 写回答

2条回答 默认 最新

  • douyou2368 2016-12-04 17:52
    关注

    You can use an html <form> element at client side

    <form action="download.php">
      Username: <input type="text" name="username"><br>
      Password: <input type="password" name="password"><br>
      File name: <input type="text" name="filename"><br>
      <input type="submit" value="Download">
    </form>
    

    $file_url = "ftp://" 
                . $_GET["username"] 
                . ":" 
                . $_GET["password"] 
                . "@124.23.148.103/124.23.148.103 port 25665/" 
                . $_GET["filename"];
    header("Content-Type: application/octet-stream");
    header("Content-Transfer-Encoding: Binary"); 
    header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\""); 
    readfile($file_url);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题