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
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
1378
52
Explain InnoDB Full-Text Search capabilities.
Apr 02, 2025 pm 06:09 PM
InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.
How do you alter a table in MySQL using the ALTER TABLE statement?
Mar 19, 2025 pm 03:51 PM
The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.
How do I configure SSL/TLS encryption for MySQL connections?
Mar 18, 2025 pm 12:01 PM
Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]
When might a full table scan be faster than using an index in MySQL?
Apr 09, 2025 am 12:05 AM
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.
What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)?
Mar 21, 2025 pm 06:28 PM
Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]
How do you handle large datasets in MySQL?
Mar 21, 2025 pm 12:15 PM
Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.
Can I install mysql on Windows 7
Apr 08, 2025 pm 03:21 PM
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.
Difference between clustered index and non-clustered index (secondary index) in InnoDB.
Apr 02, 2025 pm 06:25 PM
The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values and pointers to data rows, and is suitable for non-primary key column queries.


