Displaying images stored in a database using PHP -
for current assignment i'm trying store files db , display them. have no problem storing files can't display them properly. i'm using in main form.
<td><a href=getimage.php?id='.$file[0].'" />resume</a></td>
and query i'm using file out of db.
$sql = "select file table 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™-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 sample @ moment finished php should allow display pdf documents. if it's i'm using phpmyadmin , mysqli.
as comments suggest, storing file location in database better storing file's data in database. if need store file contents in database reason, there 2 methods. 1 using html data: url , other having php file middle man.
for php middle man method, @ answer above mine sets header information , echo's file contents. file extension doesn't matter since browsers refer content-type header instead of file extensions.
for data url method, can placed directly in code 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 strings instead of file path $finfo = new finfo(fileinfo_mime_type); $mime = $finfo->buffer($row['file']); // check if mime 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.'" />'; ?>
Comments
Post a Comment