PHP 回显来自 MariaDB BLOB 的图像

Job*_*bbo 5 php blob mariadb

我有一个 PHP/MariaDB 应用程序,其中一些图像存储为 BLOB。我最近将该应用程序移至云托管服务,并且查看图像的行为被破坏。PHP 服务于图像的 GET 请求是这样的:

    require(__DIR__.'/../include/db/MyDatabase.php');
    
    try {
        $data = (new MyDatabase())->getImage(htmlspecialchars($date));
        header('Content-Type:'.$data['mime']);
        header('Content-Length:'.strlen($data['image']));
        echo $data['image'];
    }catch(Exception $e){
        header('Content-Type:application/json; charset=UTF-8');
        echo json_encode([ 'errors' => [ $e->getMessage() ]]);
    }
Run Code Online (Sandbox Code Playgroud)

$data是一个关联数组,包含图像的 mime 类型、二进制和图像的日期。当我在服务器上托管该应用程序时,效果很好。这是带有 PHP 8.1 和 MySQL PDO 驱动程序 mysqlnd 8.1.2-1ubuntu2.14 的 Ubuntu 22.04。

在云托管服务上,他们使用 PHP 8.2 和 MySQL PDO 驱动程序 3.1.21,并且图像不起作用。内容长度和类型设置:

内容长度和类型

但图像不显示:

破碎的形象

我不知道不同的版本是否会有所不同,但我对可能出现的问题一无所知。

编辑:仅供比较,左侧的请求会返回图像,右侧的请求则不会:

浏览器开发工具

我真的不知道要寻找什么,但是两种环境中都有相同的代码

有谁知道可能出了什么问题,或者去哪里寻找?

Luu*_*uuk 0

使用 litespeed 进行测试,效果如下:

<?php
header('Content-Type: image/png');
//header('Content-Length: '.filesize("test.png"));
$data = readfile("test.png");
// echo $data;  // EDIT: This is wrong because it outputs the size of the picture, and that was not supposed to happen..
exit;
Run Code Online (Sandbox Code Playgroud)

看来你不需要发送Content-Length

我注意到一种奇怪的行为......

执行时curl http://localhost/test/image.php --output test2.png,我的输出图像比输入图像大 5 个字节。

检查发现,原始图像的长度(21215)已添加到 test2.png 文件中。

root@ubuntu45:/usr/local/lsws/Example/html/test# hexdump -C test2.png | tail -3
000052d0  71 9d ec 00 00 00 00 49  45 4e 44 ae 42 60 82 32  |q......IEND.B`.2|
000052e0  31 32 31 35                                       |1215|
000052e4
root@ubuntu45:/usr/local/lsws/Example/html/test# hexdump -C test.png | tail -3
000052c0  04 1a 81 16 00 00 00 01  26 fd ff 10 06 28 5b a5  |........&....([.|
000052d0  71 9d ec 00 00 00 00 49  45 4e 44 ae 42 60 82     |q......IEND.B`.|
000052df
Run Code Online (Sandbox Code Playgroud)

编辑:我评论了 PHP 中的其中一行(并向其添加了注释),因为它正在输出图像的长度。

测试使用:LiteSpeed/1.7.18 Open (BUILD built: Tue Aug 29 12:59:39 UTC 2023)在 Ubuntu 22.04.3 上