Home  >  Article  >  Backend Development  >  PHP implements the method of uploading pictures and saving them to the database, _PHP tutorial

PHP implements the method of uploading pictures and saving them to the database, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:06:48817browse

php implements the method of uploading images and saving them to the database,

PHP implements the method of uploading images and saving them to the database. Share it with everyone for your reference. The specific analysis is as follows:

PHP uploads images, generally using the move_uploaded_file method to save them on the server. But if a website has multiple servers, it needs to publish the images to all servers for normal use (except those using image servers)
If the image data is saved in the database, files can be shared among 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.
The 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 Max 65K
MediumBlob Max 16M
LongBlob Maximum 4G

Data table photo, used to save image data, the structure is as follows:

Copy code The code 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 ;

upload_image_todb.php:

Copy code The code is as follows:
// 连接数据库 
$conn=@mysql_connect("localhost","root","")  or die(mysql_error()); 
@mysql_select_db('demo',$conn) or die(mysql_error()); 

// 判断action 
$action = isset($_REQUEST['action'])? $_REQUEST['action'] : ''; 

// 上传图片 
if($action=='add'){ 
    $image = mysql_escape_string(file_get_contents($_FILES['photo']['tmp_name'])); 
    $type = $_FILES['photo']['type']; 
    $sqlstr = "insert into photo(type,binarydata) values('".$type."','".$image."')"; 
    @mysql_query($sqlstr) or die(mysql_error()); 
    header('location:upload_image_todb.php'); 
    exit(); 
// 显示图片 
}elseif($action=='show'){ 
    $id = isset($_GET['id'])? intval($_GET['id']) : 0; 
    $sqlstr = "select * from photo where id=$id"; 
    $query = mysql_query($sqlstr) or die(mysql_error()); 
    $thread = mysql_fetch_assoc($query); 
    if($thread){ 
        header('content-type:'.$thread['type']); 
        echo $thread['binarydata']; 
        exit(); 
    } 
}else{ 
// 显示图片列表及上传表单 
?> 
 
 
  
   
  upload image to db demo  
  
 
  
 

 
 

图片:

 
 

 
 
 
 
    $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 '

'; 
    } 
?> 
 
 

?>

希望本文所述对大家的php程序设计有所帮助。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/957141.htmlTechArticlephp实现上传图片保存到数据库的方法, php实现上传图片保存到数据库的方法。分享给大家供大家参考。具体分析如下: php 上传图片,一般...
Statement:
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