2011-09-01 49 views
0

我有一個上傳表單MySQL的文件上傳鏈接

文檔上傳到(上傳/參考/)文件夾和文檔的鏈接被存儲在MySQL數據庫是這樣的:

$sql="INSERT INTO uploaddate (upload_id, file_link) 
      VALUES 
      ('$upload_id', '$target')"; 

鏈路存儲在「uploaddata」表中的「file_link」列是這樣的:

(上傳/參考/ 6206webhost change.txt)

現在,當我運行選擇語句該表中,研究所只是顯示純文本擴展ead我想獲得一個可點擊的鏈接,可用於從它的位置下載上傳的文件。

我怎樣才能做到這一點?

回答

1

你可以這樣做:當你使用PHP 渲染頁面時,創建一個鏈接<a href="/download.php?id=doc_id">Download</a>
然後在一個新的頁面調用的download.php獲取文檔ID,從數據庫使用DOC_ID讀取文件名和文件發送出去,以便用戶可以下載...

編輯:

  • $_GET['id']你得到doc_id:小心,檢查值是否真的存在,並且sanitize it
  • 查詢你的db;例如SELECT file_link FROM uploaddate WHERE upload_id = doc_id;
  • 看看到接受的答案here

我寫其他職位的例子,但檢查得到阿拉信息:

if (file_exists($file)) { 
    header('Content-Description: File Transfer'); 
    header('Content-Type: application/octet-stream'); 
    header('Content-Disposition: attachment; filename='.basename($file)); 
    header('Content-Transfer-Encoding: binary'); 
    header('Expires: 0'); 
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
    header('Pragma: public'); 
    header('Content-Length: ' . filesize($file)); 
    ob_clean(); 
    flush(); 
    readfile($file); 
    exit; 
} 
+0

能否請您闡述的download.php網頁代碼? ? – ABI

+0

@ABI:嘿,我不能爲你寫所有的代碼!無論如何,看看我編輯的帖子... – Marco