dqypcghd381390 2016-05-11 13:49
浏览 24
已采纳

如何使用HTML5从非Web文件夹下载?

I have a webpage for that is for internal use only. It is going to be hosted on a server that is primarily running other processes that generate log files in the /opt/appname/logs/ dir. I have been tasked with making a web interface that will allow these log files to be downloaded to any computer on our network. I am using Apache on Debian. This is what I have so far:

<details>
   <summary><b>Download Log Files</b></summary>
   <?php
   foreach (glob("/opt/appname/logs/*.log") as $filename) {
      $file_info = explode("/logs/", $filename);
      ?>
      <a href="<?php echo $filename ?>" download><?php echo $file_info[1] ?></a>
      <br>
   <?php } ?>
</details>

When I try and download a log file the download looks like it starts but then gives me the message "Failed No file". I have found many posts on line that say you can or can't do this but none of them provide adequate examples. I realize that there are issues with accessing anything outside of the web folders but there has to be some way to do this. I am really at a loss here so any suggestions would be great. Lastly I am self taught so if you see any errors or things that I am not doing best practice please let me know. I am always trying to improve.

  • 写回答

2条回答 默认 最新

  • douse8732 2016-05-11 14:04
    关注

    You could create a file download.php. This file can look something like:

    Download.php

    $fn = $_GET["filename"];
    
    if (is_readable($fn)) {
        header('Content-Description: File Transfer');
        header('Content-Type: text/plain');
        header('Content-Disposition: attachment; filename="'.$fn.'"');        
        readfile($fn);
    } else {
       //return a 404 error perhaps.
    }
    

    You could then modify your original listing code to:

    <details>
       <summary><b>Download Log Files</b></summary>
       <?php
       foreach (glob("/opt/appname/logs/*.log") as $filename) {
          $file_info = explode("/logs/", $filename);
          ?>
          <a href="download.php?filename=<?php echo $filename ?>" download><?php echo $file_info[1] ?></a>
          <br>
       <?php } ?>
    </details>
    

    I personally like this method because you can also add extra checks and exceptions in the "download manager".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线