douxingsuo8809 2014-03-24 17:29
浏览 40
已采纳

使用PHP显示存储在数据库中的图像

For my current assignment I'm trying to store files into the DB and display them. Currently I have no problem storing the files but I can't display them properly. I'm using this in my main form.

<td><a href=getImage.php?id='.$file[0].'"  />Resume</a></td>

And this is the query I'm using to get the file out of the DB.

  $sql = "SELECT file FROM table WHERE Res_ID=$referenced_ID";
  $result = mysqli_query($dbc, $sql);
  $row = mysqli_fetch_assoc($result);

header('Content-type: image/jpg');
     echo "<img src=\"{$row['file']}\" /><br />";

The result is

…³V!µÔ¢ošweöÿZ–îÌèEÈÎpEJ·˜kä€òþòâ­Gas È*G¨¥vA¤uEN¤S]‰:ñY“iwØ8‡¥e]ØÝGÑ‘øQÍ!«3¨ÄvÙÁu§]zÕÿOã^ssÌuáY7WP“ÔT6Æà™ëâþÊQË!üi­oe8 9ô5ã?ÛSÇÔ‘RÃ⛘VCBh¹>ϳ=BïÃVwåçÔW#¯|8†ufŠ4ob1PiÞ=q(,>µÐYx®Æ÷¥ò›Ñ5b½äy6«ðÆbͲϡ®fóá– í„ξ‹’xvîYVEöæ«6§]|²½ýj‡Ï#åËßjv™-k.u\ŠÈ›O–#ó!ЊúÊmcLþ-¹ïšÆÔ4ïkùñE¸ÿ0h±j§‘òù Ԉ켯íº×ÂÝã/g:»¸âïþ=³-²û?áHÑI3ŒKÙW¨ÍNºŽî-¶_Ã@IÙ¸{Tiû¸>â€3ÿu7@V[àü­š‘í•ùT[9\Ó’ª?oçJ©ù…69Ùxß÷¹©ö¼ |ÊOµ°™W#@êàÔ²iw®»–uõ5Eíåˆà«ôÅHä¯"‘\PˆÙŽ>ïÖœÖò'¡÷†^Ž@3Sg+ÀéYžt±uBG­>=H'ÞB>”À¾7ƒè)É#)åsU×S‰‡R>µ"\£ô4‹é"ç=D/ÿ- š¦¼ô©àuCó'´‰ Ë7çPëÑ¿Z½åÛÌ>\Mk &ʱœÒ0ê*#)ÏAV怩#Bd`^):ܲ¯ü*îÛERk†^ù¦CiÃsõ¦ ší¹Îj¬“*øhf﶑¬†CqíH’ëëIæsŒŠÐ{=ÍW{ :dÓ¸aÐœýjXîæCÃA:æ”CŽÿe5iG£}h7I7ßCüê¬8èqP–t<Ò…©£¸MÄúbª¼EM'ÚJš‘oPýôϽHˆ2GµZ¶¾T H/Ò¢yb“§ò¦R9ÓC6,n...

I'm using images as a sample at the moment but the finished php should eventually allow to display PDF documents. If it's any help I'm using phpmyadmin and MySQLi.

  • 写回答

2条回答 默认 最新

  • dourang8305 2014-03-24 17:41
    关注

    As the comments suggest, storing the file location in the database is much better than storing the file's data in the database. BUT if you need to store the file contents in the database for some reason, there are two methods. One is using the HTML data: URL and the other is having a PHP file as the middle man.

    For the PHP Middle Man method, look at the answer above mine where it sets the header information and then echo's the file contents. The file extension doesn't matter since browsers refer to the content-type header instead of file extensions.

    For the Data URL method, which can be placed directly in your code easily check out http://en.wikipedia.org/wiki/Data_URI_scheme

    <?php
        // Array of valid Mime Types, prevents possible XSS methods.
        $valid_mimes = array(
            'image/png',
            'image/gif',
            'image/jpeg'
        );
    
        // Obtain Mime Type using finfo
        // Finfo allows for strings instead of file path
        $finfo = new finfo(FILEINFO_MIME_TYPE);
        $mime = $finfo->buffer($row['file']);
    
        // Check if mime is in our $valid_mimes array
        if(!in_array($mime,$valid_mimes)) {
            // Handle Error
            echo 'Illegal Mime Type: '.$mime;
            die();
        }
    
        $b64 = base64_encode($row['file']);
        echo '<img src="data:'.$mime.';base64,'.$b64.'" />';
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊