从MySQL DB下载文件

时间:2013-03-29 10:27:49

标签: php mysql upload download

我似乎无法在线下载我上传到MySQL数据库中的文件我只能获得纯文本,模糊的标志。在localhost虽然下载没问题。我在线查看了我的数据库,并且本地上传的数据都很好。我将向你展示Upload.php(它不是真的称之为,而且这也不是整个脚本,它是一个很大的脚本)首先:

if (isset($_POST['verstuur'])) {
$id = NULL;
if($_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}
$query = "INSERT INTO upload (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";
mysql_query($query) or die('Fout, query mislukt'); 
$query = "SELECT LAST_INSERT_ID() ID";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$id = $row['ID'];
}

}

好的,我有另一个脚本,我在其中创建下载链接:

$query = "SELECT U_ID, name FROM upload WHERE U_ID = '$opvraag[U_ID]'";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
 echo "<td>Geen Bijlage</td>";
} 
else
{
 while(list($id, $name) = mysql_fetch_array($result))
 {
     echo '<td><a href="download.php?id='.$opvraag['U_ID'].'">'.$name.'</a></td>';
 }
}

最后我有download.php

if(isset($_GET['id'])) 
{
$id    = $_GET['id'];
$query = "SELECT name, type, size, content " .
     "FROM upload WHERE U_ID = '$id'";
$result = mysql_query($query) or die('Fout, query mislukt');
list($name, $type, $size, $content) =  mysql_fetch_array($result);
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo stripslashes($content);
exit;
}

数据库表:
名称:类型:
U_ID int(8)AUTO_INCREMENT
name varchar(255)
size int(8)
输入文字
内容blob

1 个答案:

答案 0 :(得分:0)

您可以尝试使用以下内容替换download.php中的3个headers()来电:

    header("Accept-Ranges: bytes");
    header("Keep-Alive: timeout=15, max=100");
    header("Content-Disposition: attachment; filename=$name");
    header("Content-type: $type");
    header("Content-Transfer-Encoding: binary");
    header( "Content-Description: File Transfer");