How to store images in a database in php

Source: Internet
Author: User
How to store images in a database in php

How to store images in a database in php

If you want to directly store binary data, such as image files and HTML files, in your MySQL database, this article is for you! I will show you how to store these files through HTML forms and how to access and use these files.
Overview:
. Create a new database in mysql
. Example program for storing files
. Example program on how to access a file
Create a new database in mysql
First, you must create a new database in your mysql database. We will store those binary files in this database. In this example, I will use the following structure. To create a database,
Follow these steps:
. Go to 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); "(line cannot be broken)
If there is no accident, the database and table should be created.
Example program for storing files
In this example, you can use Html forms to transfer files to the database.
Store. php3
// Store. php3-by Florian Dittmer
?>
// If a 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)
[Next 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, the new data storage form is displayed.
?>
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, and then click Submit.
After the file is uploaded to the web server, the program will tell you the ID of the file you just uploaded. Remember this ID and you will need it later.
Example program on how to access a file
You can use this program to access the files you just stored.
// Getdata. php3-by Florian Dittmer
// Call method: getdata. php3? Id =
If ($ id ){
// You may need to adjust the host name, user name, 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 the file to be accessed, and you must take 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 an image in a database, you can call it like calling an image.
Example: The ID of an image file in the database is 3. You can call it as follows:
How to store files larger than 1 MB:
If you want to store files larger than 1 MB, you must make many modifications to your programs, PHP settings, and SQL settings ,.
The following several items may help you store files smaller than 24 MB:
1. Modify store. php3 and change MAX_FILE_SIZE to 24000000.
2. Modify Your PHP settings. In general, PHP only allows files smaller than 2 MB. You must change the value of max_filesize (in php. ini) to 24000000.
3. Remove the MYSQL data packet size limit. In general, MYSQL data packets smaller than 1 MB.
4. You must use the following parameters to restart your MYSQL instance.
/Usr/local/bin/safe_mysqld-O key_buffer = 16 M-O table_cache = 128-O sort_buffer = 4 M-O record_buffer = 1 M-O max_allowed_packet = 24 M
5. If the error persists:
It may be a timeout error. If you store a large file through a very slow connection, the default PHP time limit is 30 seconds. You can change the value of max_execution_time (in php. ini) to-1.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.