设为首页收藏本站language 语言切换
查看: 1304|回复: 0
收起左侧

用PHP实现把图象上传到数据库

[复制链接]
发表于 2010-2-24 13:39:49 | 显示全部楼层 |阅读模式
转载请注明出处<BR><BR>  今天教给大家如何用PHP实现把图象上传到MYSQL数据库中。 在这个教程中我们需要建立3个PHP文件:<BR><BR>  readdir.php - 把图片放到数据库的代码<BR>  image.php - 显示实际图片的代码<BR>  view.php - 显示你如何调用数据库中的图片的代码<BR><BR>1.创建一个数据库<BR><BR>CREATE TABLE `images` (<BR>`imgid` INT NOT NULL AUTO_INCREMENT ,<BR>`sixfourdata` LONGTEXT NOT NULL ,<BR>RIMARY KEY ( `imgid` ) <BR>);<BR>READDIR.PHP<BR><BR>具体的内容:<BR>&lt;?<BR>$dbcnx = mysql_connect("localhost", "username", "password"); <BR>mysql_select_db("base64imgdb");<BR>?&gt;<BR>'我们需要打开一个目录 <BR>"./"<BR>'readdir.php 文件定位于这个目录: <BR>$path = "./";<BR>$dir_handle = opendir($path)_or_die("Unable to open directory $path");<BR><BR>  下面是比较难的部分,大家需要好好研究一下:把图象分类,并且读出正在使用的一些数据 <BR><BR>fopen<BR><BR>'转换 <BR><BR>base64_encode<BR><BR>' 插入到表里 <BR><BR>&lt;?<BR>while ($file = readdir($dir_handle)) {<BR>$filetyp = substr($file, -3);<BR>if ($filetyp == 'gif'_or_$filetyp == 'jpg') {<BR>$handle = fopen($path . "/" . $file,'r');<BR>$file_content = fread($handle,filesize($path . "/" . $file));<BR>fclose($handle);<BR>$encoded = chunk_split(base64_encode($file_content)); <BR>$sql = "INSERT INTO images SET sixfourdata='$encoded'"; <BR>mysql_query($sql);<BR>}<BR>}<BR>?&gt;<BR><BR>  关闭设置的目录,然后处理: <BR><BR>&lt;?<BR>closedir($dir_handle);<BR>echo("complete");<BR>mysql_close($dbcnx);<BR>?&gt;<BR><BR>  读出图片的代码:IMAGE.PHP<BR>  这段代码比较难,我们要好好看看<BR><BR>&lt;?<BR>$dbcnx = mysql_connect("localhost", "username", "password"); <BR>mysql_select_db("base64imgdb");<BR>?&gt;<BR>我们读出图片使用的代码image.php?img=x: <BR>&lt;?<BR>$img = $_REQUEST["img"];<BR>?&gt;<BR>之后我们需要连接数据库,然后读出 <BR>&lt;?<BR>$result = mysql_query("SELECT * FROM images WHERE imgid=" . $img . ""); <BR>if (!$result) { <BR>echo("&lt;b&gt;请求错误: " . mysql_error() . "&lt;/b&gt;"); <BR>exit(); <BR>} <BR>while ($row = mysql_fetch_array($result)) { <BR>$imgid = $row["imgid"];<BR>$encodeddata = $row["sixfourdata"]; <BR>}<BR>?&gt;<BR><BR>&lt;?<BR>mysql_close($dbcnx);<BR>echo base64_decode($encodeddata);<BR>?&gt;<BR><BR>  在这里我们要理解base64-encoded 图象数据格式。<BR><BR>  "让我们来看看具体的图片吧!" VIEW.PHP <BR><BR>image.php?img=1<BR><BR>image.php?img=357 <BR><BR>&lt;img src='image.php?img=1' border="0" alt=""&gt;<BR><BR>看看完整的一个例子吧!<BR><BR>readdir.php: <BR>&lt;?<BR>###############################<BR># DB CONNECTION<BR># CHANGE THESE VALUES <BR>###############################<BR>$dbcnx = mysql_connect("localhost", "username", "password"); <BR>mysql_select_db("base64imgdb");<BR><BR>$path = "./";<BR>$dir_handle = opendir($path)_or_die("Unable to open directory $path");<BR>while ($file = readdir($dir_handle)) {<BR>$filetyp = substr($file, -3);<BR>if ($filetyp == 'gif'_or_$filetyp == 'jpg') {<BR>$handle = fopen($file,'r');<BR>$file_content = fread($handle,filesize($file));<BR>fclose($handle);<BR>$encoded = chunk_split(base64_encode($file_content)); <BR>$sql = "INSERT INTO images SET sixfourdata='$encoded'"; <BR>mysql_query($sql);<BR>}<BR>}<BR><BR>closedir($dir_handle);<BR>echo("complete");<BR>mysql_close($dbcnx);<BR>?&gt;<BR>image.php: <BR>&lt;?<BR>$dbcnx = mysql_connect("localhost", "username", "password"); <BR><BR>mysql_select_db("base64imgdb");<BR><BR>$img = $_REQUEST["img"];<BR><BR>$result = mysql_query("SELECT * FROM images WHERE imgid=" . $img . ""); <BR><BR>if (!$result) { <BR><BR>echo("&lt;b&gt;Error performing query: " . mysql_error() . "&lt;/b&gt;"); <BR>exit(); <BR>} <BR>while ($row = mysql_fetch_array($result) ) { <BR>$imgid = $row["imgid"];<BR>$encodeddata = $row["sixfourdata"]; <BR>}<BR>mysql_close($dbcnx);<BR>echo base64_decode($encodeddata);<BR>?&gt;<BR>And view.php (i shouldnt need to post this..) <BR>&lt;html&gt;<BR>&lt;body&gt;<BR>..<BR>&lt;img src='image.php?img=1' border="0" alt=""&gt;<BR>..<BR>&lt;/body&gt;<BR>&lt;/html&gt;<BR>
您需要登录后才可以回帖 登录 | 论坛注册

本版积分规则

QQ|Archiver|手机版|小黑屋|sitemap|鸿鹄论坛 ( 京ICP备14027439号 )  

GMT+8, 2025-4-6 21:07 , Processed in 0.070840 second(s), 24 queries , Redis On.  

  Powered by Discuz!

  © 2001-2025 HH010.COM

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