How to store images directly in the MySQL database if you want to store binary data, such as image files and HTML files, directly 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, you must perform the following steps:
The following is a reference clip:
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.
An example of how to store files. with this example, you can transmit files to the database through Html forms ..
Store. php3 is referenced below:
The following is a reference clip:
// Store. php3-by Florian Dittmer
?>
Store binary data into SQL Database
// 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 )".
"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.
?>
}
?>