Detailed explanation of examples of php storing images in mysql
If you want to save binary data, such as image files and HTML files, directly in your MySQL database, then this article is for you! I'll show you how to store these files through HTML forms, and how to access and use these files.
Overview of this article:
. Create a new database in mysql
. An example program of how to save files
. An example program of how to access files
Create a new database in mysql
First, you must create a new database in your mysql. We will store those binary files in this database. In the example I will use the following structure. In order to create the database,
you must do the following steps:
. Enter the MySql controller
. Enter the command "create database binary_data;"
. Enter the command "use binary_data;"
. Enter the command
"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));" (Cannot break lines)
If there are no accidents, the database and table should be established.
An example program of how to store files
Using this example, you can transfer files to the database through Html forms.
store.php3
// store.php3 - by Florian Dittmer
?>
// If the form is submitted, the code will be executed:
if ($submit) {
// Connect to the database
// (You may need to adjust the host name, user name and password)
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)
[Continue from the previous line:] VALUES ($form_description,$data,$form_data_name,$form_data_size,$form_data_type)");
$id= mysql_insert_id();
print "
This file has the following Database ID: $id";
MYSQL_CLOSE();
} else {
// Otherwise, display the form to store new data
?>
File Description:
File to upload/store in database:
}
?>
If you execute this program, you will see a simple Html form, click Browse to select a file, then click Submit.
When the file is uploaded to the web server, the program will tell you the ID of the file just uploaded. Remember this ID for use later.
An example program of how to access files
You can access the file you just saved through this program
// getdata.php3 - by Florian Dittmer
// Call Method: getdata.php3?id=
if($id) {
// You may need to adjust the hostname, username and password:
@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;
};
?>
The program must know which file to access, and you must pass the ID as a parameter.
For example: The ID of a file in the database is 2. You can call it like this:
getdata.php3?id=2
If you store the picture in the database, you can call it like calling the picture it.
Example: An image file has an ID of 3 in the database. You can call it like this:
How to store files larger than 1MB:
If you want to store files larger than 1MB, you must configure your program , PHP settings, SQL settings to make many modifications.
The following items may help you store files smaller than 24MB:
1. Modify store.php3 and change the value of MAX_FILE_SIZE to 24000000.
2. Modify your PHP settings. Under normal circumstances, PHP only allows files smaller than 2MB. You must change the value of max_filesize (in php.ini) to 24000000
3. Remove the MYSQL packet size limit. Under normal circumstances, MYSQL is less than 1 MB data packet.
4. You must restart your MYSQL with the following parameters
/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. If the error still occurs:
It may be a timeout error. If you store a large file through a very slow connection, PHP's default time The limit is 30 seconds. You can change the value of max_execution_time (in php.ini) to -1
[Related recommendations]
2. Detailed explanation of how to write SQL statements to delete tables in different databases
3. Teach you how to use one machine to run multiple MySQL services
4. Small Mysql database backup script without virtual host
5. Detailed examples of using mysqldump to back up and restore tables
The above is the detailed content of Detailed explanation of examples of php storing images in mysql. For more information, please follow other related articles on the PHP Chinese website!

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

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

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











Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

MySQL and MariaDB can coexist, but need to be configured with caution. The key is to allocate different port numbers and data directories to each database, and adjust parameters such as memory allocation and cache size. Connection pooling, application configuration, and version differences also need to be considered and need to be carefully tested and planned to avoid pitfalls. Running two databases simultaneously can cause performance problems in situations where resources are limited.

Data Integration Simplification: AmazonRDSMySQL and Redshift's zero ETL integration Efficient data integration is at the heart of a data-driven organization. Traditional ETL (extract, convert, load) processes are complex and time-consuming, especially when integrating databases (such as AmazonRDSMySQL) with data warehouses (such as Redshift). However, AWS provides zero ETL integration solutions that have completely changed this situation, providing a simplified, near-real-time solution for data migration from RDSMySQL to Redshift. This article will dive into RDSMySQL zero ETL integration with Redshift, explaining how it works and the advantages it brings to data engineers and developers.

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

LaravelEloquent Model Retrieval: Easily obtaining database data EloquentORM provides a concise and easy-to-understand way to operate the database. This article will introduce various Eloquent model search techniques in detail to help you obtain data from the database efficiently. 1. Get all records. Use the all() method to get all records in the database table: useApp\Models\Post;$posts=Post::all(); This will return a collection. You can access data using foreach loop or other collection methods: foreach($postsas$post){echo$post->

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.
