找回密码
 注册会员
查看: 1033|回复: 0

php 图片以二进制流的形式存入数据库。并且能显示出来。

[复制链接]
发表于 2010-3-4 12:32:13 | 显示全部楼层 |阅读模式
<p>php 图片以二进制流的形式存入数据库。并且能显示出来。</p>
<p><img src="http://img.baidu.com/img/iknow/icn_point.gif"> 悬赏分:100 -</p>
<p>解决时间:2010-3-4 12:31</p>
<p>就是我想上专一些图片,想把它以二进制的形式存mysql数据库里</p>
<p>然后,我在php代码里如何写,才能把它们再读出来呢。</p>
<p>谢谢哪位大侠帮帮忙哦。</p>
<p>提问者: nono_2000 - 四级</p>
<p>最佳答案</p>
<p>怎样在mysql中存储比较大的图片?</p>
<p>如果你想把二进制的数据,比如说图片文件和HTML文件,直接保存在你的MySQL数据库,那么这篇文章就是为你而写的!我将告诉你怎样通过HTML表单来储存这些文件,怎样访问和使用这些文件。</p>
<p>一、本文概述</p>
<p>本文的主要内容如下:</p>
<p>*  在MySQL中建立一个新的数据库</p>
<p>*  一个怎样储存文件的例子程序</p>
<p>*  一个怎样访问文件的例子程序</p>
<p>二、在MySQL中建立一个新的database</p>
<p>首先,你必须在你的MySQL中建立一个新的数据库,我们将会把那些二进制文件储存在这个数据库里。在例子中我会使用下列结构,为了建立数据库,你必须做下列步骤:</p>
<p>1.  进入MySQL控制器</p>
<p>2.  输入命令"create  database  binary_data;"</p>
<p>3.  输入命令"use  binary_data;"</p>
<p>输入如下命令:</p>
<p>"CREATE  TABLE  binary_data  (  id  INT(4)  NOT  NULL  AUTO_INCREMENT  PRIMARY  KEY,description  CHAR(50),  bin_data  LONGBLOB,  filename  CHAR(50),  filesize  CHAR(50),  filetype  CHAR(50));"  (不能断行)</p>
<p>如果没有意外,数据库  和  表  应该建立好了。</p>
<p>三、一个怎样储存文件的例子程序</p>
<p>用这个例子你可以通过Html表单将文件传输到数据库中。</p>
<p>store.php3</p>
<p>//  store.php3  -  by  Florian  Dittmer</p>
<p>?></p>
<p>//  如果提交了表单,代码将被执行:</p>
<p>if  ($submit)  {</p>
<p>//  连接到数据库</p>
<p>//  (你可能需要调整主机名,用户名和密码)</p>
<p>MYSQL_CONNECT(  "localhost",  "root",  "password");</p>
<p>MySQL_select_db(  "binary_data");</p>
<p>$data  =  addslashes(fread(fopen($form_data,  "r"),  filesize($form_data)));</p>
<p>$result=MYSQL_QUERY(  "INSERT  INTO  binary_data  (description,bin_data,filename,filesize,filetype)VALUES  (\'$form_description\',\'$data\',\'$form_data_name\',\'$form_data_size\',\'$form_data_type\')");</p>
<p>$id=  MySQL_insert_id();</p>
<p>print  "This  file  has  the  following  Database  ID:  $id";</p>
<p>MYSQL_CLOSE();</p>
<p>}  else  {</p>
<p>//  否则显示储存新数据的表单</p>
<p>?></p>
<p>@MySQL_select_db(  "binary_data");</p>
<p>$query  =  "select  bin_data,filetype  from  binary_data  where  id=$id";</p>
<p>$result  =  @MYSQL_QUERY($query);</p>
<p>$data  =  @MYSQL_RESULT($result,0,  "bin_data");</p>
<p>$type  =  @MYSQL_RESULT($result,0,  "filetype");</p>
<p>Header(  "Content-type:  $type");</p>
<p>echo  $data;</p>
<p>};</p>
<p>?></p>
<p>程序必须知道要访问那个文件,  你必须将ID作为一个参数。</p>
<p>例如:  一个文件在数据库中的ID为2.  你可以这样调用它:  getdata.php3?id=2</p>
<p>如果你将图片储存在数据库里,  你可以向调用图片一样调用它。</p>
<p>Example:  一个图片文件在数据库中的ID为3.  你可以这样调用它:</p>
<p>五、怎样储存大于1MB的文件</p>
<p>如果你想储存大于1MB的文件,你必须对你的程序、PHP设置、SQL设置进行许多修改。</p>
<p>下面几条也许可以帮助你储存小于24MB的文件:</p>
<p>1)  修改  store.php3,将  MAX_FILE_SIZE  的值改成  24000000。</p>
<p>2)  修改你的PHP设置,在一般情况下,PHP只允许小于2MB的文件,你必须将max_filesize(在php.ini中)的值改成24000000</p>
<p>3)  去掉MYSQL的数据包大小限制,在一般情况下  MYSQL  小于1  MB的数据包。</p>
<p>4)  你必须用以下参数重启你的MYSQL  :/usr/local/bin/safe_MySQLd  -O  key_buffer=16M  -O  table_cache=128  -O  sort_buffer=4M  -O  record_buffer=1M  -O  max_allowed_packet=24M</p>
<p>5)  如果仍然出错:可能是超时错误,如果你通过一个很慢的连接来储存一个很大的文件,PHP缺省的时间限制为30秒。你可以将max_execution_time(在php.ini中)的值改为-1</p>
<p>下面是一个老外写的,可以读</p>
<p>Saving  Images  in  MySQL</p>
<p>Sometimes,  it\'s  more  convenient  to  save  images  in  a  database  than  as  files.</p>
<p>MySQL  and  PHP  make  it  very  easy  to  do  this.  In  this  article,  I  will  describe</p>
<p>how  to  save  images  in  a  MySQL  database  and  display  them  later  on.</p>
<p>Setting  up  the  database</p>
<p>The  difference  between  any  regular  text  or  integer  fields  and  a  field  that</p>
<p>needs  to  save  an  image  is  the  amount  of  data  that  is  needed  to  be  held  in  the</p>
<p>field.  MySQL  uses  special  fields  to  hold  large  amounts  of  data.  These  fields</p>
<p>are  known  as  blobs  (blob).</p>
<p>Here  is  the  BLOB  definition  from  the  MySQL  site  :</p>
<p>A  BLOB  is  a  binary  large  object  that  can  hold  a  variable  amount  of  data.  The</p>
<p>four  BLOB  types  TINYBLOB,  BLOB,  MEDIUMBLOB  and  LONGBLOB  differ  only  in  the</p>
<p>maximum  length  of  the  values  they  can  hold</p>
<p>For  more  information  about  MySQL  BLOBs  check  out  http://www.mysql.net/Manual_c</p>
<p>hapter/manual_Reference.html#BLOB</p>
<p>Use  the  next  syntax  to  create  a  basic  table  that  will  hold  the  images:</p>
<p>CREATE  TABLE  Images  (</p>
<p>PicNum  int  NOT  NULL  AUTO_INCREMENT  PRIMARY  KEY,</p>
<p>Image  BLOB</p>
<p>);</p>
<p>Setting  the  upload  script</p>
<p>An  example  of  a  file  upload  front  end  can  be  seen  at  File  Uploading  by  berber</p>
<p>(29/06/99).  What  we  need  now  is  the  PHP  script  that  will  get  the  file  and</p>
<p>insert  it  into  MySQL.  The  next  script  does  just  that.  In  the  script,  I\'m</p>
<p>assuming  that  the  name  of  the  file  field  is  "icture".</p>
<p><?</p>
<p>If($Picture  !=  "none")  {</p>
<p>$PSize  =  filesize($Picture);</p>
<p>$mysqlPicture  =  addslashes(fread(fopen($Picture,  "r"),  $PSize));</p>
<p>unlink($Picture);</p>
<p>mysql_connect($host,$username,$password)</p>
<p>or  die("Unable  to  connect  to  SQL  server");</p>
<p>@mysql_select_db($db)</p>
<p>or  die("Unable  to  select  database");</p>
<p>mysql_query("INSERT  INTO  Images  (Image)  VALUES  \'($mysqlPicture\')")</p>
<p>or  die("Can\'t  Perform  Query");</p>
<p>}</p>
<p>else  {</p>
<p>echo"You  did  not  upload  any  picture";</p>
<p>}</p>
<p>?></p>
<p>This  is  all  that  is  needed  to  enter  the  image  into  the  database.  Note  that  in</p>
<p>some  cases  you  might  get  an  error  when  you  try  to  insert  the  image  into</p>
<p>MySQL.  In  such  a  case  you  should  check  the  maximum  packet  size  allowed  by</p>
<p>your  MySQL  ver.  It  might  be  too  small  and  you  will  see  an  error  about  this  in</p>
<p>the  MySQL  error  log.</p>
<p>What  we  did  in  the  above  file  is  :</p>
<p>1.  Check  if  a  file  was  uploaded  with  If($Picture  !=  "none").</p>
<p>2.  addslashes()  to  the  picture  stream  to  avoide  errors  in  MySQL.</p>
<p>3.  Delete  the  temporary  file.</p>
<p>3.  Connect  to  MySQL,  choose  the  database  and  insert  the  image.</p>
<p>Displaying  the  Images</p>
<p>Now  that  we  know  how  to  get  the  images  into  the  database  we  need  to  figure</p>
<p>out  how  to  get  them  out  and  display  them.  This  is  more  complicated  than</p>
<p>getting  them  in  but  if  you  follow  these  steps  you  will  have  this  up  and</p>
<p>running  in  no  time.</p>
<p>Since  showing  a  picture  requires  a  header  to  be  sent,  we  seem  to  be  in  an</p>
<p>impossible  situation  in  which  we  can  only  show  one  picture  and  than  we  can\'t</p>
<p>show  anymore  Since  once  the  headers  are  sent  we  can\'t  send  any  more  headers.</p>
<p>This  is  the  tricky  part.  To  outsmart  the  system  we  use  two  files.  The  first</p>
<p>file  is  the  HTML  template  that  knows  where  we  want  to  display  the  image(s).</p>
<p>It\'s  a  regular  PHP  file,  which  builds  the  HTML  that  contains  the  <IMG>  tags,</p>
<p>as  we  want  to  display  them.  The  second  file  is  called  to  provide  the  actual</p>
<p>file  stream  from  the  database  directly  into  the  SRC  property  of  the  <IMG></p>
<p>tag.</p>
<p>This  is  how  a  simple  script  of  the  first  type  should  look  like:</p>
<p><HTML></p>
<p><BODY></p>
<p><?</p>
<p>mysql_connect($host,$username,$password)</p>
<p>or  die("Unable  to  connect  to  SQL  server");</p>
<p>@mysql_select_db($db)</p>
<p>or  die("Unable  to  select  database");</p>
<p>mysql_query("SELECT  *  FROM  Images")</p>
<p>or  die("Can\'t  Perform  Query");</p>
<p>While($row=mysql_fetch_object($result))  {</p>
<p>echo  "<IMG  SRC=\"SecondType.php3?PicNum=$row->icNum\">";</p>
<p>}</p>
<p>?></p>
<p></BODY></p>
<p></HTML></p>
<p>While  the  HTML  is  being  displayed,  the  SecondType.php3  file  is  called  for</p>
<p>each  image  we  want  to  display.  The  script  is  called  with  the  Picture  ID</p>
<p>(PicNum)  which  allows  us  to  fetch  the  image  and  display  it.</p>
<p>The  SecondType.php3  file  looks  like  this  :</p>
<p><?</p>
<p>$result=mysql_query("SELECT  *  FROM  Images  WHERE  PicNum=$PicNum")</p>
<p>or  die("Can\'t  perform  Query");</p>
<p>$row=mysql_fetch_object($result);</p>
<p>Header(  "Content-type:  image/gif");</p>
<p>echo  $row->Image;</p>
<p>?></p>
<p>This  is  the  whole  theory  behind  images  and  MySQL.  The  scripts  in  this  example</p>
<p>are  the  basics.  You  can  now  enhance  these  scripts  to  include  thumbnails,  set</p>
<p>the  images  in  various  positions,  enhance  the  database  table  to  hold  an  ALT</p>
<p>field,  Check  the  width  and  height  of  the  images  before  you  insert  them  into</p>
<p>the  database  and  keep  that  data  in  the  table  too  etc...</p>
<p>0</p>
<p>回答者:</p>
<p>mfktxgt - 四级   2010-3-3 16:03</p>
<p>我来评论>></p>
<p>提问者对于答案的评价:</p>
<p>搞定。谢谢哦。</p>
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

QQ|文字版|手机版|小黑屋|襄阳城

GMT+8, 2025-8-2 07:14

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表