【问题标题】:Mysql blob image has no dimensions when downloaded下载时Mysql blob图像没有尺寸
【发布时间】:2012-04-23 04:46:19
【问题描述】:

出于安全原因,我在 MYSQL 中存储了一些图像。当我下载图像时,文件以正确的文件大小和名称下载,但没有显示图像。当我查看它的属性时,图像也没有尺寸。我正在使用 cakephp。

header("Content-type: ".$file['UploadFile']['file_extension']);
header("Content-Disposition: attachment; filename=\"".$file['UploadFile']['file_name']."\"");
header("Content-length: ".$file['UploadFile']['file_size']);



echo $file['UploadFile']['file_content'];

我使用以下代码保存我的图像...

public function image_upload($fileName, $source, $extension, $file_size) {
     $this->loadModel('UploadFile');

     $content = addslashes(file_get_contents($source));

     $file_data = array('UploadFile' => array('title' => $fileName, 'file_content' => $content, 'file_name' => $fileName, 'file_extension' => $extension, 'file_size' => $file_size, 'file_type' => 'image-art'));

     $this->UploadFile->create();
     $this->UploadFile->save($file_data);

     $file_id = $this->UploadFile->id;
     return $file_id;
    }

【问题讨论】:

  • 尝试将$file['UploadFile']['file_extension']打印出来,返回什么?
  • 您的问题提出了两个不同的概念 - 从数据库中提取图像 - 从文件上传中提取图像。你在说哪个?
  • 对不起,我在mysql中的表名是upload_files,所以对于cakephp,您可以使用模型UploadFile访问数组中的数据
  • @DRP96 它返回图像/jpeg。

标签: php mysql image file cakephp


【解决方案1】:

你可能有这样的几个可能的原因

A.错误的内容类型 .. 你的代码中有 Content-type: ".$file['UploadFile']['file_extension'] .. 那是磨损的`

例如。文件扩展格式类似于“.jpg”,而内容类型为image/jpeg

B.您的文件可能未正确保存,这一定会导致此类图像损坏

C.文件必须在上传过程中被截断

D. $content = addslashes(file_get_contents($source)); .. 永远不要 addslashes 到图像 .. 当你加载图像时它会搞砸或准备好 stripslashes

要在下载之前知道您的图像是否有效,您可以运行此代码

$image = ImageCreateFromString($file['UploadFile']['file_content']);
if(!$image)
     Something is Wrong 

您也可以使用getimagesize 来添加额外的验证

编辑 1

概念证明

$image = ImageCreateFromString ( $file ['UploadFile'] ['file_content'] );
if ($image) {

    /**
     * Check If Height and Width is grater than 1
     */
    if (ImageSX ( $image ) > 1 && ImageSY ( $image ) > 1) {
        $extention = strtolower ( $file ['UploadFile'] ['file_extension'] );
        switch ($extention) {
            case "png" :
                header ( 'Content-Type: image/png' ); // This is just example
                imagepng ( $image );
                break;

            case "gif" :
                header ( 'Content-Type: image/gif' ); // This is just example
                imagegif ( $image );
                break;

            case "jpg" :
            case "jpeg" :
                header ( 'Content-Type: image/jpg' ); // This is just example
                imagejpeg ( $image );
                break;

            default :
                die ( "Unsupported Image" );
                break;

        }

    } else {
        die ( "Fake Image" );
    }
} else {
    die ( "Invalid Image -- Contains Errors" );
}}

【讨论】:

  • A.) 我的 file_extension 实际上是 mime 类型,我需要更改数据库中的字段名称。 B.)我添加了用于保存上面文件的代码。 C.)我使用的是 longblob,所以我不确定它如何被截断,而且下载的文件完全是空白的,没有任何权限
  • A) 在您添加图片之前,图片的尺寸是否有效? B)$content = addslashes(file_get_contents($source)); 永远不要在图像上添加斜线.....它会搞砸......
  • 似乎都是 add_slashes,我需要在我的字段定义中添加更多字符
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多