2014-12-05 20 views
1

我有一個下載腳本的問題..所以我做了一個上傳腳本 - >它將文件添加到名爲Uploads的文件夾中,並插入id,文件名並鏈接到數據庫。但我找不到下載上傳文件的方法。這裏是我的下載腳本:我的腳本下載了頁面的源文件,而不是實際的文件

<?php 
    include('config.php'); 
    $dwquery = mysqli_query($db, "SELECT id, filename FROM files"); 

    $id = intval($_GET['id']); 
     while ($row = mysqli_fetch_array($dwquery)) { 
      echo '<a href="download.php?id='.$row['id'].'">'.$row['filename'].'</a><br />'; 
     } 

     $download = mysqli_query($db, "SELECT link FROM files WHERE id=$id"); 
     $link = mysqli_fetch_array($download); 

     if($id != '') { 
     header('Content-disposition: attachment; filename='.$link[0]); 
     readfile($_SERVER['DOCUMENT_ROOT'] . '/project/' .$link[0]); 
     } 

    ?> 

當我選擇哪個文件下載,它只是下載網頁的源代碼,而不是實際的文件。

編輯:它實際上下載了download.php的源代碼+我想下載的文件內的文本/代碼。因此它的回聲:

echo '<a href="index.php"> Index </a>/<a href="upload.php"> Upload </a>/<a href="download.php"> Download </a>/<a href="logout.php"> Logout </a><br />'; 

而下載的文件是:

<a href="index.php"> Index </a>/<a href="upload.php"> Upload </a>/
<a href="download.php"> Download </a>/<a href="logout.php"> Logout 
</a><br /><a href="download.php?id=6">test1.php</a><br /><a 
href="download.php?id=5">test2.txt</a><br /> 

and here's the actual code of the file 

回答

0

你的代碼讀取這樣的:

Show list of files 
IF a download id is provided: 
    Show the content of that file 

應該這樣寫:

IF a download id is provided 
    Show the content of that file 
ELSE 
    Show list of files 

由於您沒有錯誤檢查文件的存在,您可能也會錯誤地獲取文件的路徑。

+0

那麼路徑肯定沒有錯。我嘗試上傳一個帶有一些文本的.txt文件,然後我下載了它。下載的文件包含如下內容:網站的菜單以及文本文件中的文本。 – tsvmitev 2014-12-05 21:36:04

相關問題