简体   繁体   English

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

[英]Displaying images stored in a database using 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Ë!üioe8 9ô5ã?ÛSÇÔ'RÃ⛘VCBh¹>ϳ=BïÃVwåçÔW#¯|8†ufŠ4ob1PiÞ=q(,>µÐYx®Æ÷¥ò›Ñ5b½äy6«ðÆbͲϡ®fóá– í„ξ‹'xvîYVEöæ«6§]|²½ýj‡Ï#åËßjv™-ku\\ŠÈ›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– …³V! !üioe89ô5ãÛSÇÔ'RÛ›VCBh¹>ϳ=BïÃVwåçÔW#¯| 8†ufŠ4ob1PiÞ= q(,> µÐYx®Æ÷¥ò›Ñ5b½äyy6«ðÆbͲϮ®fóá–í¿ å½ýj‡Ï#åËßjv™-ku \\ŠÈ› 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 /ÿ-š Ατελείστοςπ7ôPéÑ¿ ëëIŒsŒŠÐ{=ÍW{:dÓ¸aÐœýjXîæCÃA:æ”CŽÿe5iG£}h7I7ßCüê88qq– t<Ò…©£¸MÄúbª¼EM'ÚJš'oPýôϽHˆ2GµZ¶¾T H/Ò¢yb“§ò¦R9ÓC6,n... 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. 我目前使用图像作为示例,但是完成的php最终应该允许显示PDF文档。 If it's any help I'm using phpmyadmin and MySQLi. 如果有帮助,我正在使用phpmyadmin和MySQLi。

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. 一种是使用HTML数据:URL,另一种是使用PHP文件作为中间人。

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. 对于PHP Middle Man方法,请查看我上面的答案,在该答案中设置标头信息,然后回显文件内容。 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 对于可以直接放置在代码中的数据URL方法,请轻松查看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.'" />';
?>

You cant output <img src=\\ as an output it will corrupt the image file and Please use getImage.php as a different file to output images or verify that no output is printed before,after or in mid of image else it will corrupt the image. 您不能输出<img src=\\作为输出,它会损坏图像文件,请使用getImage.php作为其他文件来输出图像,或验证图像之前,之后或之中没有打印输出,否则它将损坏图像。图片。

if(isset($_GET['id'])){
$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 $row['file'];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM