用PHP和MySQL保存和输出图片
设置数据库
我们通常在数据库中所使用的文本或整数类型的字段和需要用来保存图片的字段的不同之处就在于两者所需要保存的数据量不同。MySQL数据库使用专门的字段来保存大容量的数据,数据类型为BLOB。
MySQL数据库为BLOB做出的定义如下:BLOB数据类型是一种大型的二进制对象,可以保存可变数量的数据。BLOB具有四种类型,分别是TINYBLOB,BLOB, MEDIUMBLOB 和LONGBLOB,区别在于各自所能够保存的最大数据长度不同。
在介绍了所需要使用的数据类型之后,我们可以使用以下语句创建保存图象的数据表。
CREATE TABLE Images ( PicNum int NOT NULL AUTO_INCREMENT PRIMARY KEY, Image BLOB );
编写上传脚本
关于如何实现文件的上传,我们在这里就不再介绍了,感兴趣的读者可以参见“网页陶吧”内的相关文章。现在,我们主要来看一下如何接收上传文件并将其存入到MySQL数据库中。具体的脚本代码如下,其中我们假定文件上传域的名称为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("Cant Perform Query");
}else {
echo"You did not upload any picture";
}
?>
这样,我们就可以成功的把图片保存到数据库中。如果在将图片插入MySQL的过程中出现问题,可以检查一下MySQL数据库所允许的最大数据包的大小。如果设置值过小的话,我们会在数据库的错误日志中找到相应的记录。
下面,我们简单说明一下上述脚本程序。首先,我们通过“If($Picture != "none")”检查是否有文件被上传。然后,使用addslashes()函数避免出现数据格式错误。最后,连接MySQL,选择数据库并插入图片。
显示图片
在知道了如何将图? 入数据库之后,我们就需要考虑怎样才能从数据库中取出图片并在HTML页面中显示出来。这个过程要稍微复杂一些,下面我们就来介绍一下实现过程。
因为PHP显示图片需要发送相应的标头,所以我们就会面临这样一个问题,那就是一次只能显示一副图片,因为我们无法在发出标头之后再发送其它的标头。
为了有效的解决这一问题,我们编写了两个文件。其中,第一个文件作为HTML页面的模板,定位图片的显示位置。第二个文件则被用来从数据库中实际输出文件流,作为标签的SRC属性。
第一个文件的简单形式可以如下:
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("Cant Perform Query");
While($row=mysql_fetch_object($result)) {
echo "
} ?>
当HTML页面被浏览时,每显示一副图片就会调用一次Second.php3文件。当第二个文件被调用时会传入相应的Picture ID,我们可以借此从数据库中取回对应的图片并显示。
Second.php3文件如下:
$result=mysql_query("SELECT * FROM Images WHERE PicNum=$PicNum") or die("Cant perform Query");
$row=mysql_fetch_object($result);
Header( "Content-type: image/gif");
echo $row->Image;
?>
到此,我们就介绍完了使用PHP和MySQL保存和显示图片的全过程。文中所举得都是一些最简单的实例,读者可以根据自己的实际需要加入其它一些功能,使整个程序更加完善。

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



MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

Navicat itself does not store the database password, and can only retrieve the encrypted password. Solution: 1. Check the password manager; 2. Check Navicat's "Remember Password" function; 3. Reset the database password; 4. Contact the database administrator.

Create a database using Navicat Premium: Connect to the database server and enter the connection parameters. Right-click on the server and select Create Database. Enter the name of the new database and the specified character set and collation. Connect to the new database and create the table in the Object Browser. Right-click on the table and select Insert Data to insert the data.

You can create a new MySQL connection in Navicat by following the steps: Open the application and select New Connection (Ctrl N). Select "MySQL" as the connection type. Enter the hostname/IP address, port, username, and password. (Optional) Configure advanced options. Save the connection and enter the connection name.

Common reasons why Navicat cannot connect to the database and its solutions: 1. Check the server's running status; 2. Check the connection information; 3. Adjust the firewall settings; 4. Configure remote access; 5. Troubleshoot network problems; 6. Check permissions; 7. Ensure version compatibility; 8. Troubleshoot other possibilities.

Common errors and solutions when connecting to databases: Username or password (Error 1045) Firewall blocks connection (Error 2003) Connection timeout (Error 10060) Unable to use socket connection (Error 1042) SSL connection error (Error 10055) Too many connection attempts result in the host being blocked (Error 1129) Database does not exist (Error 1049) No permission to connect to database (Error 1000)

To connect to a local MySQL database using Navicat: Create a connection and set the connection name, host, port, username, and password. Test the connection to make sure the parameters are correct. Save the connection. Select a new connection from the connection list. Double-click the database you want to connect to.

Steps to perform SQL in Navicat: Connect to the database. Create a SQL Editor window. Write SQL queries or scripts. Click the Run button to execute a query or script. View the results (if the query is executed).
