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 cplex运行后参数报错是为什么
  • ¥15 之前不小心删了pycharm的文件,后面重新安装之后软件打不开了
  • ¥15 vue3获取动态宽度,刷新后动态宽度值为0
  • ¥15 升腾威讯云桌面V2.0.0摄像头问题
  • ¥15 关于Python的会计设计
  • ¥15 聚类分析 设计k-均值算法分类器,对一组二维模式向量进行分类。
  • ¥15 stm32c8t6工程,使用hal库
  • ¥15 找能接spark如图片的,可议价
  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥15 博通raid 的写入速度很高也很低