在MySQL存储并获取图片,store and retrieve imgs in MySQL
2016-11-24 09:55
337 查看
本文:
以blob形式在mysql存储图片,并在网页展示。
试了一晚上。。亲测有效(´-ι_-`)
以blob形式在mysql存储图片,并在网页展示。
试了一晚上。。亲测有效(´-ι_-`)
/* 递交图片页面 */ <html> image: <input type="file" name="file" id="file"/> </html> <?php $db = mysqli_connect("localhost","username","password") or die(mysqli_connect_error()); mysqli_select_db($db, "bcit")or die(mysqli_error($db)); //获取图片信息 $file=$_FILES["file"]["tmp_name"]; $size = filesize($file); $image = addslashes(fread (fopen($file, "r"), $size)); //创建image table $query=" CREATE TABLE IF NOT EXISTS image( id MEDIUMINT NOT NULL AUTO_INCREMENT, image LONGBLOB, PRIMARY KEY(id))"; $result=mysqli_query($db, $query) or die(mysqli_error($db)); //插入图片 $query="INSERT INTO image(id, image) VALUES (NULL, '$image')"; $result=mysqli_query($db, $query) or die(mysqli_error($db)); ?> /* 展示图片页面 */ <?php //链接数据库同上,假设所获取图片的id为$id $query="SELECT * FROM image WHERE id='$id'"; $result=mysqli_query($db, $query) or die(mysqli_error($db)); if(mysqli_num_rows($result)!=0){ while ($row = mysqli_fetch_assoc($result)){ $imageData=$row['image']; echo '<img src="data:image/jpeg;base64,'.base64_encode($imageData).'" style="width:100px; height:100;"/>'; } ?>
相关文章推荐
- PHP using mcrypt and store the encrypted in MySQL
- Store and retrieve objects in SQL Server
- 获取MySQL加密密码并验证用户输入(登录界面),retrieve hashpwd from mysql and compare with input password on login
- 将viewstate存储在数据库中并压缩 附代码(store viewstate in database and compress)
- 往MySQL中存储图片
- Participate in MySQL-GlassFish Student Contest and Win $500
- MySQL如何存储图片?---一个向数据库存取image文件的jsp程序
- SQL SELECT TOP N equivalent in ORACLE and MySQL
- Sams Teach Yourself PHP, MySQL and Apache All in One (3rd Edition) NEW RELEASE
- Sams teach yourself PHP, MySQL and Apache in 24 hours
- Participate in MySQL-GlassFish Student Contest and Win $500
- install mysql and gcc in linux as 4.5 use INSTALL-BINARY version
- 图片存储与浏览一例(Linux+Apache+PHP+MySQL)
- RHEL5上对mysql进行图片等文件存储例子
- Store binary data to MySQL using escape_string in MySQLdb
- Participate in MySQL-GlassFish Student Contest and Win $500
- How To Use ADO.NET to Retrieve and Modify Records in an Excel Workbook With Visual Basic .NET(利用Ado.net导出到Excel)
- 往MySQL中存储图片
- Dates in PHP and MySQL
- Sams Teach Yourself PHP, MySQL and Apache All in One (3rd Edition)