2012-08-12 10 views
1

내 데이터베이스에서 ID에 의해 지정된 "다운로드"링크가있는 테이블을 반환합니다. 사용자가 "다운로드"를 클릭하고 파일 다운로드를 시작한다는 알림을받습니다. 지금은 "다운로드"를 클릭 할 수 있고 내가 다운로드 한 파일이 원하는 파일이 아니라면, 그것은 PHP 페이지의 메아리입니다.PHP로 헤더 파일을 강제 다운로드

Profile.php

<?PHP 

function filedownload() 
{ 
       header("Content-Type: application/msword"); 
       header("Content-Length: ". $row['size']); 
       header("Content-Disposition: attachment; filename=".$row['title']); 
       header("Content-Transfer-Encoding: binary"); 
       readfile($thefile, 1); 

} 
       ?> 

"다운로드"는 테이블에서 링크 파일의 ID에 제대로 대응 download.php

<?php if (isset($_POST['query'])) 
    { 
    require_once ('../mysqli_connect.php'); //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 




        // Make sure an ID was passed DOWNLOAD HANDLER ******* 
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 
     $downloadq = " 
      SELECT `file_type`, `size`, `title`, 'content', 'upload_id' 
      FROM `upload` 
      WHERE `upload_id` =".$id; 
     $result = mysqli_query ($dbc, $downloadq); // Run the query 


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

       $place = './uploads/'.$_SESSION_['email'].'/'; 
       $thefile = $place.$row['title']; 

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


       // Print headers 
       // header("Content-Type: application/msword"); 
       // header("Content-Length: ". $row['size']); 
       // header("Content-Disposition: attachment; filename=".$row['title']); 
       // header("Content-Transfer-Encoding: binary"); 
       // readfile($thefile); 



       // Print data 
       //echo (stripslashes($row['content'])); 



       exit; 


      } 
      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); 



    } 

        ?> 

: 여기

는 현재 코드 데이터베이스에. "다운로드"를 클릭하면 profile.php라는 파일이 다운로드되고 php 및 html 마크 업이 포함됩니다. 미리 감사드립니다.

+0

올바른 파일 이름을 가지고 있는지 확인하기 위해 filededload 함수에서 $ thefile을 반향 시도하십시오.이 코드를 사용하기 전에 글로벌 변수 값 – wgcrouch

+3

에 의존하는 대신 파일 이름과 $ row 배열을 전달하십시오. [SQL 주입 공격] (http://bobby-tables.com) 및 코드를 수정하십시오. 그러면 서버가 pwn3d가됩니다. –

+0

예, 바운드 매개 변수를 사용합니다. – V1GG3N

답변

1

에 한번 당신의하면 FileDownload() 내부의 첫 번째 라인으로

global $row, $thefile; 

퍼팅 - 기능. 이 함수 밖에서 변수를 사용하려고합니다.

+0

시도해 보았습니다. 결과는 내 데이터베이스에 해당하는 올바른 제목의 파일을 다운로드한다는 것입니다. 그러나 파일의 내용은 내 프로필 페이지의 html 마크 업이며 텍스트 상자와 헤더가 있습니다. – V1GG3N

+1

지금 주목 : 다운로드 링크는'newpub_profile.php'라는 파일을 참조합니다. 그것이 상위 코드의 맨 아래 부분입니까? –

+0

해당 링크가 간과되었습니다. newpub_profile.php는 위 코드의 첫 번째 블록을 포함하는 파일의 이름입니다. "profile.php"라고합니다. 이 링크는 직접 download.php를 가리켜 야합니까? – V1GG3N