将文件作为Blob插入后错误FileType - PHPMySQL

时间:2022-09-23 08:43:17

I made a simple script to insert files as BLOB (mediumblob) in MySQL Database. The script works fine, the file is uploaded and saved into the table but when I download the file and I try to open it, it says: "File type HTML document (text/html) is not supported"!

我做了一个简单的脚本,在MySQL数据库中插入BLOB(mediumblob)文件。脚本工作正常,文件上传并保存到表中但是当我下载文件并尝试打开它时,它说:“不支持文件类型HTML文档(text / html)”!

This means there was an error while saving the file's type!

这意味着保存文件类型时出错!

Here is my code, please tell me what can be wrong in it:

这是我的代码,请告诉我它可能有什么问题:

upload.php :

if (isset($_POST['upload'])) 
{
if(isset($_POST['upload']) && $_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);
}
$p = $cnx->prepare('INSERT INTO commandes (name, size, type, content) VALUES(:name, :size, :type, :content)');
$p->execute(array('name'=>$fileName, 'size'=>$fileSize, 'type'=>$fileType, 'content'=>$content));    
echo "<br>File $fileName uploaded<br>";
}
}

Download.php :

$p = $cnx->prepare('SELECT cmd_id, name FROM commandes');
$p->setFetchMode(PDO::FETCH_OBJ);
$p->execute();

if($p->rowCount() == 0)
{
echo "0 Element <br />";
}
else
{
while($data = $p->fetch())
{
?>
<a href="./downloadpdf.php?id=<?php echo $data->cmd_id;?>"><?php echo $data->name;?></a> <br>
<?php
}
}

if(isset($_GET['id']))
{

$id = $_GET['id'];
$q = $cnx->prepare('SELECT * FROM commandes WHERE cmd_id = :cmd_id');
$q->setFetchMode(PDO::FETCH_OBJ);
$q->execute(array('cmd_id'=>$id));
while($getFile = $q->fetch())
{
header("Content-length: $getFile->size");
header("Content-type: $getFile->type");
header("Content-Disposition: download; filename=$getFile->name");
echo $getFile->pdf;

exit;
}
}

Thank you!

1 个解决方案

#1


0  

What is the output for the response headers?

响应头的输出是什么?

Can you ensure that "Content-type" is "Content-Type"

你能确保“内容类型”是“内容类型”吗?

Also, using a debugger to inspect the response is really valuable.

此外,使用调试器检查响应非常有价值。

http://fiddler2.com/get-fiddler

#1


0  

What is the output for the response headers?

响应头的输出是什么?

Can you ensure that "Content-type" is "Content-Type"

你能确保“内容类型”是“内容类型”吗?

Also, using a debugger to inspect the response is really valuable.

此外,使用调试器检查响应非常有价值。

http://fiddler2.com/get-fiddler