为什么下载的图片损坏了?
why downloaded image is damaged?
我想从数据库中获取 jpg 格式图像的二进制数据,并使用以下代码为其下载 link。
它在 wamp 服务器上运行,下载完成后它会打开并且文件没有损坏,它表明文件已正确存储在数据库中,但在真实服务器上不起作用,并且已下载 link 但下载完成后文件无法打开。
我尝试将它用于 pdf 文件,它可以在服务器和 wampserver 上运行。
在下面的代码中 $row 被正确填充,我确信数据库值没有问题。
$content 是图像的二进制值。
服务器需要特殊设置吗?
$sql = "SELECT * FROM files WHERE file_id= ? ";
$params = array((int)$_POST["btn_save_file"]);
$table = sqlsrv_query( $conn, $sql, $params);
$row = sqlsrv_fetch_array( $table, SQLSRV_FETCH_ASSOC);
$message=sqlsrv_errors();
$content =$row["file_data"];
$temp = tmpfile();
fwrite($temp,$content);
$name="img".rand(1,1000).".jpg";
$a=fstat ($temp);
header('Content-type: image/jpg');
header('Content-Length: '.$a["size"]);
header("Content-Disposition: attachment; filename=".$name);
echo $content;
fclose($temp);
Charlotte Dunois 在评论中发送的答案是我的解决方案(禁用输出缓冲)。
我在下载 link 之前使用了 "ob_end_clean()",并且成功了。
我想从数据库中获取 jpg 格式图像的二进制数据,并使用以下代码为其下载 link。
它在 wamp 服务器上运行,下载完成后它会打开并且文件没有损坏,它表明文件已正确存储在数据库中,但在真实服务器上不起作用,并且已下载 link 但下载完成后文件无法打开。
我尝试将它用于 pdf 文件,它可以在服务器和 wampserver 上运行。
在下面的代码中 $row 被正确填充,我确信数据库值没有问题。
$content 是图像的二进制值。
服务器需要特殊设置吗?
$sql = "SELECT * FROM files WHERE file_id= ? ";
$params = array((int)$_POST["btn_save_file"]);
$table = sqlsrv_query( $conn, $sql, $params);
$row = sqlsrv_fetch_array( $table, SQLSRV_FETCH_ASSOC);
$message=sqlsrv_errors();
$content =$row["file_data"];
$temp = tmpfile();
fwrite($temp,$content);
$name="img".rand(1,1000).".jpg";
$a=fstat ($temp);
header('Content-type: image/jpg');
header('Content-Length: '.$a["size"]);
header("Content-Disposition: attachment; filename=".$name);
echo $content;
fclose($temp);
Charlotte Dunois 在评论中发送的答案是我的解决方案(禁用输出缓冲)。
我在下载 link 之前使用了 "ob_end_clean()",并且成功了。