2012-05-17 24 views
0

我目前正在託管網站文件之前重命名文件。我在用戶下載文件之前重命名文件時遇到問題。當文件上傳時,原始名稱保存在mysql數據庫中,文件被重命名爲一個id。下載PHP

<?php 

    require("includes/inc.php"); 

$id = trim(sanitize($_GET['id'])); //Receives the id of the file from the url 

if($id) { 
    $fileid = mysql_query("SELECT * FROM files WHERE id = '$id'"); 

    if (mysql_num_rows($fileid) != 1) { 
     header('Location: download.php?error=invalid_file'); 
     exit(); 
    } else { 
     while($info = mysql_fetch_array($fileid)) { 
     $fileid2 = $info['id']; 
     $filename = $info['name']; 
     $ext = $info['ext']; 
     $filesize = $info['size']; 
     $downloads = $info['downloads']; 
      header('Content-Description: File Transfer'); 
      header('Content-Type: application/octet-stream'); 
      header("Location: uploads/".$fileid2.".".$ext); 
      header('Content-Disposition: attachment; filename="'.$filename.'"'); 
      header('Content-Length: ' . $filesize); 
     } 
?> 
+0

我希望'$ fileid2','$ filename'和'$ filesize'被存儲到數據庫中,以防止HTTP頭注射之前消毒。 – sarnold

+4

是什麼問題?我沒有看到任何代碼發送文件到客戶端,但否則沒有什麼引人注目的...... – sarnold

+0

您在'header'中使用'$ fileid2'。如果我正確地理解了你,你希望這是一個不同的名字,對嗎? –

回答

0

我想你「而」循環,輸出會是這樣的:(我還交換了$文件名和$ fileid2在頭)

while($info = mysql_fetch_array($fileid)) { 
      $fileid2 = $info['id']; 
      $filename = $info['name']; 
      $ext = $info['ext']; 
      $filesize = $info['size']; 
      $downloads = $info['downloads']; 
    } 

    header('Content-Description: File Transfer'); 
    header('Content-Type: application/octet-stream'); 
    header('Content-Disposition: attachment; filename="'.$fileid2.'.'.$ext.'"'); 
    header('Content-Length: ' . $filesize); 

readfile(uploads/".$filename.".".$ext); 

我沒有測試這一點,所以我希望它有幫助。