0

我似乎无法在线下载我要上传到 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)
type text
content blob

4

1 回答 1

0

您可以尝试用以下代码替换您headers()的 download.php 中的 3 个调用:

    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");
于 2013-03-29T11:27:56.697 回答