图像不能显示,因为它包含错误- BLOB


image cannot be displayed, becuase it contains errors - BLOB

有人能看出这段代码有什么问题吗?

$max = 10000000; 
foreach ($_FILES["file"]["error"] as $file => $error) {     
    if ($error == UPLOAD_ERR_OK) {
        if($_FILES['file']['size'][$file] < $max) {   
               $img= file_get_contents($_FILES['evidence']['tmp_name'][$evidence]);
                $img= mysql_real_escape_string($img);
                $fileSize = $_FILES['file']['size'][$file];
                $fileType = $_FILES['file']['type'][$file];
                $sql = "INSERT INTO attached_evidence (id, image, size, type, date) 
                VALUES  ('$id', '$img', '$fileSize', '$fileType', NOW());";
                mysql_query($sql) or die("Error in Query: " . mysql_error());
        }
        else{
            echo 'Too big';
        }
    }
}

当我使用…将blob输出到另一个页面时

$query = "SELECT * FROM test_image";
    $result = @mysql_query($query)  or die ("query failed: " . mysql_error());   
    if($row = mysql_fetch_assoc($result)){          
  header('Content-type: image/'.$row['type'].'');
$blob_data = $row['image'];
    echo $blob_data;
        
            }
    else {
            
echo "No images were found";
        }

…无法显示图像,因为它包含错误。

任何想法?

感谢

编辑

   VALUES  ('$id', '$imgData', 'fileSize', 'fileType', NOW());";

你的文件类型总是"fileType",你的大小总是"fileSize"(这在数据库中是否正确存储?)因此,输出的图像类型总是image/fileType,这至少是您的问题之一。

您似乎也试图在那里使用finfo,但最终只是使用来自$_FILES数组的用户提供的MIME类型,您永远不应该这样做。

在将值放入SQL查询之前也没有转义值。addslashesmysql_real_escape_string或预备语句不同。

你也应该习惯失败的早期,而不是无尽的嵌套的if s:

if ($error !== UPLOAD_ERR_OK) {
    echo 'fail';
    continue;
}
if ($_FILES['file']['size'][$file] > $max) {
    echo 'fail';
    continue;
}
...