1. <legend id='RoHsj'><style id='RoHsj'><dir id='RoHsj'><q id='RoHsj'></q></dir></style></legend>
    2. <small id='RoHsj'></small><noframes id='RoHsj'>

        • <bdo id='RoHsj'></bdo><ul id='RoHsj'></ul>
        <i id='RoHsj'><tr id='RoHsj'><dt id='RoHsj'><q id='RoHsj'><span id='RoHsj'><b id='RoHsj'><form id='RoHsj'><ins id='RoHsj'></ins><ul id='RoHsj'></ul><sub id='RoHsj'></sub></form><legend id='RoHsj'></legend><bdo id='RoHsj'><pre id='RoHsj'><center id='RoHsj'></center></pre></bdo></b><th id='RoHsj'></th></span></q></dt></tr></i><div id='RoHsj'><tfoot id='RoHsj'></tfoot><dl id='RoHsj'><fieldset id='RoHsj'></fieldset></dl></div>
        <tfoot id='RoHsj'></tfoot>
      1. 如何在 PHP 中强制下载文件

        How to force a file to download in PHP(如何在 PHP 中强制下载文件)
          <tbody id='pw3Yy'></tbody>

      2. <i id='pw3Yy'><tr id='pw3Yy'><dt id='pw3Yy'><q id='pw3Yy'><span id='pw3Yy'><b id='pw3Yy'><form id='pw3Yy'><ins id='pw3Yy'></ins><ul id='pw3Yy'></ul><sub id='pw3Yy'></sub></form><legend id='pw3Yy'></legend><bdo id='pw3Yy'><pre id='pw3Yy'><center id='pw3Yy'></center></pre></bdo></b><th id='pw3Yy'></th></span></q></dt></tr></i><div id='pw3Yy'><tfoot id='pw3Yy'></tfoot><dl id='pw3Yy'><fieldset id='pw3Yy'></fieldset></dl></div>

          • <legend id='pw3Yy'><style id='pw3Yy'><dir id='pw3Yy'><q id='pw3Yy'></q></dir></style></legend>

                <tfoot id='pw3Yy'></tfoot>

                • <bdo id='pw3Yy'></bdo><ul id='pw3Yy'></ul>

                  <small id='pw3Yy'></small><noframes id='pw3Yy'>

                  本文介绍了如何在 PHP 中强制下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有图片列表,我想要一个下载"链接以及每张图片,以便用户可以下载图片.

                  I have list of images and I want a "Download" link along with every image so that user can download the image.

                  那么有人可以指导我如何为 php 中的任何文件提供下载链接吗?

                  so can someone guide me How to Provide Download link for any file in php?

                  编辑

                  我希望在单击下载链接时显示下载面板我不想导航到要在浏览器上显示的图像

                  I want a download panel to be displayed on clicking the download link I dont want to navigate to image to be displayed on the browser

                  推荐答案

                  如果你想强制下载,你可以使用类似下面的方法:

                  If you want to force a download, you can use something like the following:

                  <?php
                      // Fetch the file info.
                      $filePath = '/path/to/file/on/disk.jpg';
                  
                      if(file_exists($filePath)) {
                          $fileName = basename($filePath);
                          $fileSize = filesize($filePath);
                  
                          // Output headers.
                          header("Cache-Control: private");
                          header("Content-Type: application/stream");
                          header("Content-Length: ".$fileSize);
                          header("Content-Disposition: attachment; filename=".$fileName);
                  
                          // Output file.
                          readfile ($filePath);                   
                          exit();
                      }
                      else {
                          die('The provided file path is not valid.');
                      }
                  ?>
                  

                  如果您只是使用普通链接链接到此脚本,则会下载该文件.

                  If you simply link to this script using a normal link the file will be downloaded.

                  顺便说一句,上面的代码片段需要在页面开始时执行(在任何标题或 HTML 输出发生之前.)如果您决定基于此创建一个用于下载任意文件的函数,也要小心- 您需要确保防止目录遍历(realpath 是方便),如果您接受来自 $_GET 或 $_POST 的输入,则仅允许从定义的区域内下载.

                  Incidentally, the code snippet above needs to be executed at the start of a page (before any headers or HTML output had occurred.) Also take care if you decide to create a function based around this for downloading arbitrary files - you'll need to ensure that you prevent directory traversal (realpath is handy), only permit downloads from within a defined area, etc. if you're accepting input from a $_GET or $_POST.

                  这篇关于如何在 PHP 中强制下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Is PHP or PHP based web framework stateful or stateless?(PHP 或基于 PHP 的 Web 框架是有状态的还是无状态的?)
                  How to parse django style template tags(如何解析 django 样式模板标签)
                  What is a good setup for editing PHP in Emacs?(在 Emacs 中编辑 PHP 的好设置是什么?)
                  How to check whether specified PID is currently running without invoking ps from PHP?(如何在不从 PHP 调用 ps 的情况下检查指定的 PID 当前是否正在运行?)
                  What#39;s the difference between escapeshellarg and escapeshellcmd?(escapeshellarg 和escapeshellcmd 有什么区别?)
                  php in background exec() function(php 后台 exec() 函数)
                • <legend id='LiPgH'><style id='LiPgH'><dir id='LiPgH'><q id='LiPgH'></q></dir></style></legend><tfoot id='LiPgH'></tfoot>
                    <bdo id='LiPgH'></bdo><ul id='LiPgH'></ul>
                      <tbody id='LiPgH'></tbody>

                        <small id='LiPgH'></small><noframes id='LiPgH'>

                        <i id='LiPgH'><tr id='LiPgH'><dt id='LiPgH'><q id='LiPgH'><span id='LiPgH'><b id='LiPgH'><form id='LiPgH'><ins id='LiPgH'></ins><ul id='LiPgH'></ul><sub id='LiPgH'></sub></form><legend id='LiPgH'></legend><bdo id='LiPgH'><pre id='LiPgH'><center id='LiPgH'></center></pre></bdo></b><th id='LiPgH'></th></span></q></dt></tr></i><div id='LiPgH'><tfoot id='LiPgH'></tfoot><dl id='LiPgH'><fieldset id='LiPgH'></fieldset></dl></div>