| 域名空间 下载中心 社区论坛 信息公告 my小屋 |
![]() |
联系我们 设为首页 加入收藏 |
|
首页 | 新闻资讯 | 编程开发 | 网页设计 | 图形图象 | 网络媒体 | 网站模板 | 数 据 库 | 投稿 论坛 | 操作系统 | 系统优化 | 网络安全 | 黑客技术 | 硬件学堂 | 硬件报价 | 服 务 器 | 地图 专题 | 应用软件 | 聊天通讯 | q q 专栏 | 建站经验 | 在线工具 | 站长club | 注 册 表 | 旧版 社会 | 游戏娱乐 | 设计欣赏 | 疑难解答 | 社区论坛 | 韩国素材 | 素材图库 | 广告服务 | 服务 |
| 新版上线![旧版] | |||||
注:打开慢时请稍等
怎么将图片存入mysql数据库?http://www.iyit.net 日期:2006-10-6 15:39:50 来源:iyit.net收集 点击: |
怎么将图片存入mysql数据库? --------------------------------------------------------------- 贴一篇文章你自己看一下吧。 怎样插入图象进mysql? 发信站: bbs 水木清华站 (thu jul 15 13:25:46 1999) www-post 【 在 weffen (wef) 的大作中提到: 】 : craete table pic(intro text,image longblob) : sql 语句怎么写? 这是php+mysql的例子,应该满足你的要求吧. saving images in mysql sometimes, it's more convenient to save images in a database than as files. mysql and php make it very easy to do this. in this article, i will describe how to save images in a mysql database and display them later on. setting up the database the difference between any regular text or integer fields and a field that needs to save an image is the amount of data that is needed to be held in the field. mysql uses special fields to hold large amounts of data. these fields are known as blobs (blob). here is the blob definition from the mysql site : a blob is a binary large object that can hold a variable amount of data. the four blob types tinyblob, blob, mediumblob and longblob differ only in the maximum length of the values they can hold for more information about mysql blobs check out http://www.mysql.net/manual_c hapter/manual_reference.html#blob use the next syntax to create a basic table that will hold the images: create table images ( picnum int not null auto_increment primary key, image blob ); setting the upload script an example of a file upload front end can be seen at file uploading by berber (29/06/99). what we need now is the php script that will get the file and insert it into mysql. the next script does just that. in the script, i'm assuming that the name of the file field is "picture". <? if($picture != "none") { $psize = filesize($picture); $mysqlpicture = addslashes(fread(fopen($picture, "r"), $psize)); unlink($picture); mysql_connect($host,$username,$password) or die("unable to connect to sql server"); @mysql_select_db($db) or die("unable to select database"); mysql_query("insert into images (image) values '($mysqlpicture')") or die("can't perform query"); } else { echo"you did not upload any picture"; } ?> this is all that is needed to enter the image into the database. note that in some cases you might get an error when you try to insert the image into mysql. in such a case you should check the maximum packet size allowed by your mysql ver. it might be too small and you will see an error about this in the mysql error log. what we did in the above file is : 1. check if a file was uploaded with if($picture != "none"). 2. addslashes() to the picture stream to avoide errors in mysql. 3. delete the temporary file. 3. connect to mysql, choose the database and insert the image. displaying the images now that we know how to get the images into the database we need to figure out how to get them out and display them. this is more complicated than getting them in but if you follow these steps you will have this up and running in no time. since showing a picture requires a header to be sent, we seem to be in an impossible situation in which we can only show one picture and than we can't show anymore since once the headers are sent we can't send any more headers. this is the tricky part. to outsmart the system we use two files. the first file is the html template that knows where we want to display the image(s). it's a regular php file, which builds the html that contains the <img> tags, as we want to display them. the second file is called to provide the actual file stream from the database directly into the src property of the <img> tag. this is how a simple script of the first type should look like: <html> <body> <? mysql_connect($host,$username,$password) or die("unable to connect to sql server"); @mysql_select_db($db) or die("unable to select database"); mysql_query("select * from images") or die("can't perform query"); while($row=mysql_fetch_object($result)) { echo "<img src=\"secondtype.php3?picnum=$row->picnum\">"; } ?> </body> </html> while the html is being displayed, the secondtype.php3 file is called for each image we want to display. the script is called with the picture id (picnum) which allows us to fetch the image and display it. the secondtype.php3 file looks like this : <? $result=mysql_query("select * from images where picnum=$picnum") or die("can't perform query"); $row=mysql_fetch_object($result); header( "content-type: image/gif"); echo $row->image; ?> this is the whole theory behind images and mysql. the scripts in this example are the basics. you can now enhance these scripts to include thumbnails, set the images in various positions, enhance the database table to hold an alt field, check the width and height of the images before you insert them into the database and keep that data in the table too etc... -- ※ 来源:·bbs 水木清华站 bbs.net.tsinghua.edu.cn·[from: 202.117.17.24] 编辑:黑鹰 [发送给好友] [打印本页] [关闭窗口] [返回顶部] 上一篇:怎样让输入的文章内容折行? 下一篇:如何实现从一个页面自动导向另外一个页面??? 转载请注明来源:www.iyit.net 特别声明: 本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。 |
| 最新更新 | 热点排行 | 推荐新闻 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 设置首 页 - 版权声明 - 广告服务 - 关于我们 - 联系我们 - 友情连接 |
| |||||||