Heim > php教程 > php手册 > php将图片以二进制存到mysql中的方法

php将图片以二进制存到mysql中的方法

WBOY
Freigeben: 2016-06-06 19:45:33
Original
707 Leute haben es durchsucht

很奇怪,直接用file_get_contents存入数据库成功后,结果读取的时候图片不能显示,后来解决方法是什么!!是用了下base64编码。。。不多说直接上图。 1.这是upload.php ? php include ('./conn.php' ); if ( $_POST ['submit' ]) { if ( $_FILES ['image']['

很奇怪,直接用file_get_contents存入数据库成功后,结果读取的时候图片不能显示,后来解决方法是什么!!是用了下base64编码。。。不多说直接上图。

1.这是upload.php

<span>php
</span><span>include</span>('./conn.php'<span>);
</span><span>if</span> (<span>$_POST</span>['submit'<span>]) {
    </span><span>if</span> (<span>$_FILES</span>['image']['size'<span>]) {
        </span><span>$names</span> = <span>$_FILES</span>['image']['name'<span>];
        </span><span>$arr</span>   = <span>explode</span>('.', <span>$names</span><span>);
        </span><span>$name</span>  = <span>$arr</span>[0]; <span>//</span><span>图片名称</span>
        <span>$date</span>  = <span>date</span>('Y-m-d H:i:s'); <span>//</span><span>上传日期</span>
        <span>$fp</span>    = <span>fopen</span>(<span>$_FILES</span>['image']['tmp_name'], 'rb'<span>);
        </span><span>$type</span>  = <span>$_FILES</span>['image']['type'<span>];
        </span><span>$file_uploads</span> = <span>file_get_contents</span>(<span>$_FILES</span>['image']['tmp_name'<span>]);
        </span><span>$file_uploads</span> = <span>base64_encode</span>(<span>$file_uploads</span><span>);
        </span><span>if</span> (!<span>$fp</span><span>) {
            showInfo(</span>'读取图片失败!'<span>);
        } </span><span>else</span><span> {
            
            </span><span>if</span> (<span>$image</span><span>) {

                </span><span>$q</span>      = "insert into image (name, pic, type, date) values ('<span>$name</span>','<span>$file_uploads</span>','<span>$type</span>','<span>$date</span>')"<span>;
                </span><span>$result</span> = <span>mysql_query</span>(<span>$q</span><span>);

                </span><span>if</span> (<span>$result</span><span>) {
                    showInfo(</span>'上传成功!'<span>);
                } </span><span>else</span><span> {
                    showInfo(</span>'上传失败!'<span>);
                }
                </span><span>mysql_close</span>(<span>$link</span><span>);
            } </span><span>else</span><span> {
                showInfo(</span>'请选择要上传的文件!'<span>);
            }
        }
        
    } </span><span>else</span><span> {
        showInfo(</span>'请选择要上传的文件!'<span>);
    }
}


</span><span>function</span> showInfo(<span>$info</span><span>)
{
    </span><span>echo</span> "<meta http-equiv='\"Content-Type\"' content='\"text/html;' charset='utf-8\"'>"<span>;
    </span><span>echo</span> "<meta http-equiv="refresh" content="1;url=show.php">"<span>;
    </span><span>echo</span> ""<span>;
    </span><span>echo</span> "" . <span>$info</span> . "……"<span>;
    </span><span>echo</span> ""<span>;
}
</span>?>
Nach dem Login kopieren


2.image.php用于从数据库中读取图片

<span>php
</span><span>include</span>('./conn.php'<span>);

</span><span>$id</span>     = <span>$_GET</span>['id'<span>];
</span><span>$sql</span>    = "select * from image where id='<span>$id</span>'"<span>;
</span><span>$result</span> = <span>mysql_query</span>(<span>$sql</span><span>);
</span><span>if</span> (!<span>$result</span><span>)
    </span><span>die</span>("读取图片失败!"<span>);
</span><span>$num</span> = <span>mysql_num_rows</span>(<span>$result</span><span>);
</span><span>if</span> (<span>$num</span> )
    <span>die</span>("暂无图片"<span>);
</span><span>$obj</span>        = <span>mysql_fetch_object</span>(<span>$result</span><span>);
</span><span>$data</span>       = <span>base64_decode</span>(<span>$obj</span>->pic);  <span>//</span><span>Base64解码</span>
<span>$type</span> = <span>mysql_result</span>(<span>$result</span>, 0, 'type'<span>);
</span><span>header</span>("Content-type: <span>$type</span>"<span>);
</span><span>echo</span> <span>$data</span><span>;
</span>?>
Nach dem Login kopieren

3.show.php显示和上传图片

<span>php
</span><span>include</span>('./conn.php'<span>);
</span>?>
span>PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



 
 <meta http-equive="Content-Type" content="text/html" charset="utf-8">
  <title> </title>
 
 
 
Nach dem Login kopieren
php $ret = mysql_query('select * from image order by id desc'); if ($ret) { while ($row = mysql_fetch_array($ret)) { ?> php } } ?>
php将图片以二进制存到mysql中的方法
php echo $row['name']; ?>
php echo $row['date']; ?>

 

最后运行结果:

php将图片以二进制存到mysql中的方法

 

 

总结:网上的都是坑爹么?谁有其他不用base64编码的方法能否告知?

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage