2012-08-29 77 views
0

這讓我陷入了很長時間,經過大量研究,我嘗試了幾種方法。php文件下載頭文件

這是我對我的下載文件當前的代碼:

<?php 
    require_once ('./newpub_profile.php'); 
    $thetitle = $row['title']; 
    $thesize = $row['size']; 

function filedownload() { 
    GLOBAL $thesize, $thetitle; 
    header("Content-Type: application/msword"); 
    header("Content-Disposition: attachment; filename=".$thetitle); 
    header("Content-Length: ".$thesize); 
    header("Content-Transfer-Encoding: binary"); 
    readfile($realfile); 
} 
?> 

在另一個PHP頁面我填充查詢結果的表,每個結果獲得相應的下載鏈接。如果將鼠標懸停在鏈接上,我可以驗證其下載ID是否正確(在MySQL數據庫中)。下載鏈接目前指向包含上述代碼的download.php。此代碼允許我下載標題爲「download.php」的文件,其中不包含任何內容。我從用戶配置文件頁面調用函數「filedownload()」。

這是怎麼了顯示的下載鏈接:

while ($row = mysqli_fetch_array($r,MYSQLI_ASSOC)) 
     { 
      echo '<tr><td align="left">' . 
      $row['title'] . '</td><td align="left">' 
      . $row['genre'] . '</td><td align="left">' 
      . $row['length'] . '</td><td align="left">' 
      . $row['created'] . '</td><td align="left">' 
      //. $row['views'] . '</td><td align="left">' 
      . "<a href='download.php?id={$row['upload_id']}'>Download</a></td>" . '</td></tr>'; 
     } 

下面是從中調用頭功能的頁面代碼。

<?php if (isset($_POST['query'])) 
    { 
    require_once (connectionToDB); //Connect to the db 

    // Make the query 
    $genre = $_POST['select_genre']; 
    $length = $_POST['select_length']; 

    $upviews = "UPDATE upload 
       SET views = views + 1 
       WHERE genre = '$genre' AND length = '$length'"; 
    $runviewupdate = mysqli_query ($dbc, $upviews); 


    $q = "SELECT upload_id, title, genre, length, created 
     FROM upload 
     WHERE genre = '$genre' AND length = '$length' 
     ORDER BY created DESC, title DESC"; 


    $r = mysqli_query ($dbc, $q); // Run the query 

    if($r) 
    { 
     // If it ran okay, display the records 
     echo '<table align="center" 
      cellspacing="3" cellpadding="3" 
      width="75%"> 
      <tr><td align="left"><b>Title</b></td> 
      <td align="left"><b>Genre</b></td> 
      <td align="left"><b>Pages</b></td> 
      <td align="left"><b>Submitted</b></td> 
      <td align="left"><b>Download</b></td>'; 

     // Fetch and print all the records: 

     while ($row = mysqli_fetch_array($r,MYSQLI_ASSOC)) 
     { 
      echo '<tr><td align="left">' . 
      $row['title'] . '</td><td align="left">' 
      . $row['genre'] . '</td><td align="left">' 
      . $row['length'] . '</td><td align="left">' 
      . $row['created'] . '</td><td align="left">' 
      //. $row['views'] . '</td><td align="left">' 
      . "<a href='newpub_profile.php?id={$row['upload_id']}'>Download</a></td>" . '</td></tr>'; 
     } 
     echo '</table>'; // Close the table 

     mysqli_free_result ($r); // Free up the resources 
    } 
    else // If it did not run okay 
    { 
     // Public Message: 

     echo '<p class="error">Your submissions could not be retrieved. We 
      apologize for any inconvenience.</p>'; 

     // Debugging message: 

     echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p>'; 

    } // End of if ($r) IF. 



} 

//END DOWNLOAD HANDLER ****************************************************** 


    mysqli_close($dbc); // Close the database connection 



if(isset($_GET['id'])) { 
// Get the ID 
    $id = intval($_GET['id']); //var_dump($id); 

    require_once ('../mysqli_connect.php'); //Connect to the db 

// Fetch the file information 

    if($stmt = $mysqli -> prepare("SELECT `file_type`, `size`, `title`, 'content', 'upload_id' 
      FROM `upload` 
      WHERE `upload_id` =?")) { 

     /* Bind parameters 
     s - string, b - boolean, i - int, etc */ 
     $stmt -> bind_param("sissi", $file_type, $size, $title, $content, $upload_id); 

     /* Execute it */ 
     $stmt -> execute(); /*FINISH PREPARED STATEMENTS*/ 

     /* Bind results */ 
     $stmt -> bind_result($result); 

     /* Fetch the value */ 
     $stmt -> fetch(); 

     //echo $user . "'s level of priviledges is " . $result; 

     /* Close statement */ 
     //$stmt -> close(); 


     if($result) { 
      // Make sure the result is valid 
      if (mysqli_num_rows($result) > 0) { 
      // Get the row 
       $row = mysqli_fetch_assoc($result); 
       //var_dump($row); 
       //$data = $row; 

       $place = './uploads/'.$_SESSION_['email'].'/'; 
       $thefile = $place.$row['title']; 
       $realfile = $thefile; 
       $thetitle = $row['title']; 
       $thesize = $row['size']; 

       require_once('./download.php'); 
       filedownload(); 






      } 
      else { 
       echo 'Error! No such ID.'; 
      } 

      // Free the mysqli resources 
      mysqli_free_result($result); 
     } 
     else { 
      echo "Error! Query failed: <pre>{$dbc->error}</pre>"; 
     } 
     mysqli_close($dbc); 
     $stmt -> close(); 
      } 




    } 


        ?> 
+3

到底是什麼問題? – Cfreak

+0

下載鏈接不會從我的文件系統下載文件內容。相反,我的瀏覽器正在下載一個名爲「download.php」的文件,並且是一個沒有內容的空白頁面。 – V1GG3N

+0

你使用什麼瀏覽器和版本?你有沒有嘗試在其他瀏覽器?不同的瀏覽器需要不同的標題。 – Nilpo

回答

1

您創建了一個功能filedownload(),但你永遠不會調用它,所以永遠不會執行它的身體。你也從來沒有定義$realfile,你想發送文件的路徑。

調用你的函數:

<?php 
    require_once ('./newpub_profile.php'); 
    $thetitle = $row['title']; 
    $thesize = $row['size']; 

function filedownload() { 
    GLOBAL $thesize, $thetitle; 
    header("Content-Type: application/msword"); 
    header("Content-Disposition: attachment; filename=".$thetitle); 
    header("Content-Length: ".$thesize); 
    header("Content-Transfer-Encoding: binary"); 
    readfile($realfile); 
} 

filedownload(); 
?> 

儘管似乎沒有被擺在首位你有一個原因,在功能:

<?php 
    require_once ('./newpub_profile.php'); 
    $thetitle = $row['title']; 
    $thesize = $row['size']; 

    header("Content-Type: application/msword"); 
    header("Content-Disposition: attachment; filename=".$thetitle); 
    header("Content-Length: ".$thesize); 
    header("Content-Transfer-Encoding: binary"); 
    readfile($realfile); 
?> 
+0

我正在從不同的頁面(用戶個人資料頁面)呼叫我的功能。用戶配置文件頁面加載一個包含鏈接的表格,正如我在上面的代碼中更新的那樣。我希望鏈接取得我的download.php代碼並將文件提供給用戶。 – V1GG3N

+0

那麼,直接的未解決的問題仍然是缺少'$ realfile'文件的服務。你告訴它發送一個未定義變量的文件。即使你在另一個頁面中定義了'$ realfile',它也不在函數的範圍內。 –

+0

我更新了問題陳述以包含調用頁面。預先感謝您的幫助。 – V1GG3N