An example of uploading images and saving them to the database through php

jacklove
Release: 2023-03-31 08:20:01
Original
6864 people have browsed it

php Upload pictures and save them to the database example

php To upload pictures, generally use the move_uploaded_file method to save them on the server . But if a website has multiple servers, it needs to publish pictures to all servers for normal use (except those using picture servers)

If the picture data is saved to the database, File sharing can be achieved between multiple servers to save space.

First of all, the image file is binary data, so the binary data needs to be saved in the mysql database.

Mysql database provides the BLOB type for storing large amounts of data. BLOB is a binary object that can accommodate data of different sizes.

There are four types of BLOB, which are the same except for the maximum amount of information stored. Different types can be used depending on your needs.

TinyBlob Maximum 255B

Blob Maximum 65K

MediumBlob Maximum 16M

LongBlob Maximum 4G

Data tablephoto, used to save picture data, the structure is as follows:

CREATE TABLE `photo` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `type` varchar(100) NOT NULL,
  `binarydata` mediumblob NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Copy after login


upload_image_todb.php

<?php
// 连接数据库
$conn=@mysql_connect("localhost","root","")  or die(mysql_error());
@mysql_select_db(&#39;demo&#39;,$conn) or die(mysql_error());
// 判断action
$action = isset($_REQUEST[&#39;action&#39;])? $_REQUEST[&#39;action&#39;] : &#39;&#39;;
// 上传图片
if($action==&#39;add&#39;){
    $image = mysql_escape_string(file_get_contents($_FILES[&#39;photo&#39;][&#39;tmp_name&#39;]));
    $type = $_FILES[&#39;photo&#39;][&#39;type&#39;];
    $sqlstr = "insert into photo(type,binarydata) values(&#39;".$type."&#39;,&#39;".$image."&#39;)";
    @mysql_query($sqlstr) or die(mysql_error());
    header(&#39;location:upload_image_todb.php&#39;);
    exit();
// 显示图片
}elseif($action==&#39;show&#39;){
    $id = isset($_GET[&#39;id&#39;])? intval($_GET[&#39;id&#39;]) : 0;
    $sqlstr = "select * from photo where id=$id";
    $query = mysql_query($sqlstr) or die(mysql_error());
    
    $thread = mysql_fetch_assoc($query);
    
    if($thread){
        header(&#39;content-type:&#39;.$thread[&#39;type&#39;]);
        echo $thread[&#39;binarydata&#39;];
        exit();
    }
}else{
// 显示图片列表及上传表单
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <title> upload image to db demo </title>
 </head>
 <body>
  <form name="form1" method="post" action="upload_image_todb.php" enctype="multipart/form-data">
  <p>图片:<input type="file" name="photo"></p>
  <p><input type="hidden" name="action" value="add"><input type="submit" name="b1" value="提交"></p>
  </form>
<?php
    $sqlstr = "select * from photo order by id desc";
    $query = mysql_query($sqlstr) or die(mysql_error());
    $result = array();
    while($thread=mysql_fetch_assoc($query)){
        $result[] = $thread;
    }
    foreach($result as $val){
        echo &#39;<p><img src="upload_image_todb.php?action=show&id=&#39;.$val[&#39;id&#39;].&#39;&t=&#39;.time().&#39;" width="150"></p>&#39;;
    }
?>
 </body>
</html>
<?php
}
?>
Copy after login


This article explains how to upload images and save them to the database through PHP. For more related content, please pay attention to the PHP Chinese website.

Related recommendations:

How to send and receive stream files through php

How to mosaic parts of pictures through php

Explanation on how to get the file mime type through php

The above is the detailed content of An example of uploading images and saving them to the database through php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!