0

如何创建一个 PHP 脚本/页面,允许会员/买家下载存储在根目录外的下载文件夹中的压缩文件(产品)?我正在使用 Apache 服务器。请帮忙!

谢谢!保罗·G。

4

2 回答 2

1

您可能会在 @soac 提供的链接中找到一些更好的信息,但这里是我的一些 PDF 文件代码的摘录:

<?php
      $file = ( !empty($_POST['file']) ? basename(trim($_POST['file'])) : '' );
      $full_path = '/dir1/dir2/dir3/'.$file;  // absolute physical path to file below web root.
      if ( file_exists($full_path) )
      {
         $mimetype = 'application/pdf';

         header('Cache-Control: no-cache');
         header('Cache-Control: no-store');
         header('Pragma: no-cache');
         header('Content-Type: ' . $mimetype);
         header('Content-Length: ' . filesize($full_path));

         $fh = fopen($full_path,"rb");
         while (!feof($fh)) { print(fread($fh, filesize($full_path))); }
         fclose($fh);
      }
      else
      {
         header("HTTP/1.1 404 Not Found");
         exit;
      }
?>

请注意,这会在浏览器中打开 PDF,而不是直接下载 PDF,尽管您可以从阅读器中将文件保存在本地。使用readfile()可能比我在本示例中通过句柄打开文件的旧方法更有效(和更简洁的代码)。

readfile($full_path);
于 2013-05-09T22:07:24.893 回答
0

我相信您要完成的工作(通过 php 流式传输现有的 zip 文件)可以与此处的答案类似: LAMP: How to create .Zip of large files for the user on the fly, without disk/CPU thrashing


此答案的代码略微修改版本:

// make sure to send all headers first
// Content-Type is the most important one (probably)
//
header('Content-Type: application/x-gzip');

$filename = "/path/to/zip.zip";
$fp = fopen($filename, "rb");

// pick a bufsize that makes you happy
$bufsize = 8192;
$buff = '';
while( !feof($fp) ) {
   $buff = fread($fp, $bufsize);
   echo $buff;
}
pclose($fp);
于 2013-05-09T22:20:24.293 回答