PHP:如何访问根目录下的下载文件夹?

问题描述:

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

谢谢! 保罗G.

+0

使用完整路径还是相对路径? – Ohgodwhy 2013-05-09 21:54:38

+0

查看关于此链接的答案http://*.com/questions/12094080/download-files-from-server-php – soachishti 2013-05-09 21:56:14

+1

可能是一种糟糕的方式,因为您可能正在通过PHP运行一些大文件。您最好难以猜测Facebook等网址。 – 2013-05-09 22:52:25

我相信你想完成(流通过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); 
+0

看起来像这是我正在寻找的...感谢的人! – netizen0911 2013-06-16 01:15:02

您可能会发现在由@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在浏览器中而不是下载它,尽管你可以从本地保存在阅读器中的文件。使用readfile()可能会比按照我在本例中所做的方式通过句柄打开文件的旧方式更高效(并且代码更简洁)。

readfile($full_path);