2013-05-09 142 views
0

如何創建一個PHP腳本/頁面,讓成員/買家下載存儲在根目錄之外的下載文件夾中的壓縮文件(產品)?我正在使用Apache服務器。請幫忙!PHP:如何訪問根目錄下的下載文件夾?

謝謝! 保羅G.

+0

使用完整路徑還是相對路徑? – Ohgodwhy 2013-05-09 21:54:38

+0

查看關於此鏈接的答案http://stackoverflow.com/questions/12094080/download-files-from-server-php – soachishti 2013-05-09 21:56:14

+1

可能是一種糟糕的方式,因爲您可能正在通過PHP運行一些大文件。您最好難以猜測Facebook等網址。 – 2013-05-09 22:52:25

回答

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); 
+0

看起來像這是我正在尋找的...感謝的人! – netizen0911 2013-06-16 01:15:02

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在瀏覽器中而不是下載它,儘管你可以從本地保存在閱讀器中的文件。使用readfile()可能會比按照我在本例中所做的方式通過句柄打開文件的舊方式更高效(並且代碼更簡潔)。

readfile($full_path); 
相關問題