Home php教程 php手册 详解:如何将图片储存在数据库里

详解:如何将图片储存在数据库里

Jun 21, 2016 am 09:00 AM
data gt lt mysql php

如果你想把二进制的数据,比如说图片文件和HTML文件,直接保存在你的MySQL数据库,那么这篇文章就是为你而写的!我将告诉你怎样通过HTML表单来储存这些文件,怎样访问和使用这些文件。

本文概述:

。在mysql中建立一个新的数据库

。一个怎样储存文件的例子程序

。一个怎样访问文件的例子程序

在mysql中建立一个新的database

首先,你必须在你的mysql中建立一个新的数据库,我们将会把那些二进制文件储存在这个数据库里。在例子中我会使用下列结构,为了建立数据库,

你必须做下列步骤:

。进入MySql控制器

。输入命令"create database binary_data;"

。输入命令"use binary_data;"

。输入命令

"CREATE TABLE binary_data ( id INT(4) NOT NULL AUTO_INCREMENT PRIMARY KEY,description CHAR(50), bin_data LONGBLOB, filename CHAR(50), filesize CHAR(50), filetype CHAR(50));" (不能断行)

如果没有意外,数据库 和 表 应该建立好了。

一个怎样储存文件的例子程序

用这个例子你可以通过Html表单将文件传输到数据库中。

<ccid_code>store.php3<?php // store.php3 - by Florian Dittmer <dittmer@gmx.net>?><title>Store binary data into SQL Database</title>
<?php // 如果提交了表单,代码将被执行:if ($submit) {// 连接到数据库// (你可能需要调整主机名,用户名和密码)MYSQL_CONNECT( "localhost", "root", "password");mysql_select_db( "binary_data");$data = addslashes(fread(fopen($form_data,  "r"), filesize($form_data)));$result=MYSQL_QUERY( "INSERT INTO binary_data (description,bin_data,filename,filesize,filetype) [接上一行:] VALUES ('$form_description','$data','$form_data_name','$form_data_size','$form_data_type')"); $id= mysql_insert_id();   print  "<p>This file has the following Database ID: <b>$id</b>";MYSQL_CLOSE();} else {// 否则显示储存新数据的表单?><form method="post" action="<?php%20echo%20%24PHP_SELF;%20?>" enctype="multipart/form-data">File Description:<br><input type="text" name="form_description" size="40"><input type="hidden" name="MAX_FILE_SIZE" value="1000000"><br>File to upload/store in database:<br><input type="file" name="form_data" size="40"><p><input type="submit" name="submit" value="submit"></p>
</form>
<?php }?></ccid_code>
Copy after login

如果你执行了这个程序,你将会看见一个简单的Html表单,单击“浏览”选择一个文件,然后单击提交。

当文件上传至web服务器之后,程序将会告诉你刚刚上传的文件的ID,记住这个ID,待会要用的。

一个怎样访问文件的例子程序

你可以通过这个程序访问你刚才储存的文件

<ccid_code><?php // getdata.php3 - by Florian Dittmer <dittmer@gmx.net>// 调用方法: getdata.php3?id=<id>if($id) {// 你可能需要调整主机名,用户名和密码: @MYSQL_CONNECT( "localhost", "root", "password"); @mysql_select_db( "binary_data"); $query =  "select bin_data,filetype from binary_data where id=$id"; $result = @MYSQL_QUERY($query); $data = @MYSQL_RESULT($result,0, "bin_data"); $type = @MYSQL_RESULT($result,0, "filetype");Header(  "Content-type: $type");echo $data;};?></id></ccid_code>
Copy after login

程序必须知道要访问那个文件, 你必须将ID作为一个参数。

例如: 一个文件在数据库中的ID为2. 你可以这样调用它:

getdata.php3?id=2

如果你将图片储存在数据库里, 你可以向调用图片一样调用它。

Example: 一个图片文件在数据库中的ID为3. 你可以这样调用它:

怎样储存大于1MB的文件:

如果你想储存大于1MB的文件,你必须对你的程序、PHP设置、SQL设置进行许多修改,。

下面几条也许可以帮助你储存小于24MB的文件:

1、修改 store.php3 ,将 MAX_FILE_SIZE 的值改成 24000000。

2、修改你的PHP设置,在一般情况下,PHP只允许小于2MB的文件,你必须将max_filesize(在php.ini中)的值改成24000000

3、去掉MYSQL的数据包大小限制,在一般情况下 MYSQL 小于1 MB的数据包.

4、你必须用以下参数重启你的MYSQL

/usr/local/bin/safe_mysqld -O key_buffer=16M -O table_cache=128 -O sort_buffer=4M -O record_buffer=1M -O max_allowed_packet=24M

5、如果仍然出错:

可能是超时错误,如果你通过一个很慢的连接来储存一个很大的文件,PHP缺省的时间限制为30秒。你可以将max_execution_time(在php.ini中)的值改为-1



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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1261
29
C# Tutorial
1234
24
MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

The Continued Use of PHP: Reasons for Its Endurance The Continued Use of PHP: Reasons for Its Endurance Apr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

Explain the purpose of foreign keys in MySQL. Explain the purpose of foreign keys in MySQL. Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

How to safely store JavaScript objects containing functions and regular expressions to a database and restore? How to safely store JavaScript objects containing functions and regular expressions to a database and restore? Apr 19, 2025 pm 11:09 PM

Safely handle functions and regular expressions in JSON In front-end development, JavaScript is often required...

Compare and contrast MySQL and MariaDB. Compare and contrast MySQL and MariaDB. Apr 26, 2025 am 12:08 AM

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

See all articles