doujing1156 2012-04-10 21:06
浏览 28
已采纳

下载时,Mysql blob图像没有尺寸

I am storing a few images in MYSQL for security reasons. When I download the image the file downloads with the correct file size and name but no image is displayed. Also the image has no dimensions when I look at it's properties. I am using 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'];

I save my image with the following code...

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;
    }
  • 写回答

1条回答 默认 最新

  • dongru2019 2012-04-10 21:19
    关注

    They are few possible reasons you might be having such

    A. Wrong content type .. you have Content-type: ".$file['UploadFile']['file_extension'] .. that is worng` in your code

    Eg. Files extention formation is somthing like ".jpg" while the content type is image/jpeg

    B. Your file might not be properly saved which must have lead to the corruption on such image

    C. File must have been truncated during upload

    D. $content = addslashes(file_get_contents($source)); .. Don't ever addslashes to images .. it would messthing up or get ready to stripslashes when you are loading the image

    To know if your image is valid before you download you can run this code

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

    You can also use getimagesize to add additional validation

    Edit 1

    Prove of Concept

    $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" );
    }}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助