Author: Florian Dittmer translation: netmad
If you want to save binary data, such as pictorial files and html files, save directly in your MySQL database, then this article is written for you!
I will tell you how to store these files through the HTML form, how to access and use these files.
This article overview:
Create a new database in MySQL
An example of how to store files
An example of how to access a file
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 establish a 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)); "(Can't break)
If there is no accident, the database and table should be established.
An example of how to store files With this example you can transfer files into the database via HTML form. .
STORE.PHP3
PHP
// store.php3 - by florian dittmer
?>
PHP
// If the form is submitted, the code will be executed:
IF ($ SUBMIT) {
/ / Connect to the database
// (You may need to adjust the host name, username 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 folload database ID: $ ID b>";
MySQL_Close ();
} else {
/ / Otherwise, the form of the store is stored new data
?>