Save and export images with PHP and MySQL_PHP Tutorial
In the process of designing and making websites, sometimes it is more convenient to save images to a database than to save them as files. This golden combination with MySQL can easily achieve the above functions. In this article, we will introduce readers to how to save pictures to the MySQL database and how to display pictures in the database.
Set up database
The difference between the text or integer type fields we usually use in the database and the fields that need to be used to save pictures lies in the amount of data that needs to be saved. MySQL database uses special fields to save large amounts of data, and the data type is BLOB.
MySQL database defines BLOB as follows: The BLOB data type is a large binary object that can store a variable amount of data. There are four types of BLOB, namely TINYBLOB, BLOB, MEDIUMBLOB and LONGBLOB. The difference lies in the maximum data length that each can save.
After introducing the data types we need to use, we can use the following statements to create a data table to save images.
CREATE TABLE Images (PicNum int NOT NULL AUTO_INCREMENT PRIMARY KEY, Image BLOB);
Write upload script edu-cn.com
Regarding how to upload files, we will not introduce it here. Interested readers can refer to related articles in "Web Page Tao Bar". Now, we mainly look at how to receive uploaded files and store them in the MySQL database. The specific script code is as follows, in which we assume that the name of the file upload domain is Picture.
If($Picture != "none") {
$PSize = filesize($Picture);
$mysqlPicture = addslashes(fread(fopen($Picture, "r"), $PSize));
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";
}
?>
In this way, we can successfully save the image to the database. If you have problems inserting images into MySQL, you can check the maximum packet size allowed by the MySQL database. If the setting value is too small, we will find the corresponding record in the error log of the database.
Next, we briefly explain the above script program. First, we check whether a file has been uploaded by "If($Picture != "none")". Then, use the addslashes() function to avoid data format errors. Finally, connect to MySQL, select the database and insert the picture.
www.edu4u.com.cn
Show pictures
After knowing how to store images into the database, we need to consider how to retrieve images from the database and display them in HTML pages. This process is a little more complicated. Let’s introduce the implementation process below.
Because displaying images requires sending corresponding headers, we will face the problem that only one image can be displayed at a time, because we cannot send other headers after sending the header.
In order to effectively solve this problem, we have written two files. Among them, the first file serves as the template of the HTML page and locates the display position of the image. The second file is used to actually output the file stream from the database as the SRC attribute of the tag.
The simple form of the first file can be as follows:
mysql_connect($host,$username,$password) or die("Unable to connect to SQL server");
@mysql_select_db($db) or die("Unable to select database");
$result=mysql_query("SELECT * FROM Images") or die("Can't Perform Query");
While($row=mysql_fetch_object($result)) {
echo "
}
?>
www.edu4u.com.cn
When the HTML page is browsed, the Second.php3 file will be called every time an image is displayed. When the second file is called, the corresponding Picture ID will be passed in, so that we can retrieve the corresponding picture from the database and display it.
The Second.php3 file is as follows:
$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;
?>
Excerpted from Jiutian Galaxy

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
