If you want to store binary data, such as image files and HTML files, directly in your MySQL database, then this article is written for you! I'll show you how to store these files through an HTML form and how to access and use them.
This article outlines:
。 Create a new database in MySQL
。 An example program for how to store files
。 An example procedure for how to access a file
Build a new database in MySQL
First, you have to create a new database in your MySQL and we will store those binaries in this database. In the example I will use the following structure, in order to set up the database,
You must do the following steps:
。 Go to MySQL Controller
。 Enter the command "CREATE database binary_data;"
。 Enter the command "use Binary_data;"
。 Input command
"CREATE TABLE binary_data (ID INT (4) Not NULL auto_increment PRIMARY key,description CHAR (), Bin_data Longblob, Filenam e char (filesize), filetype char (50)); "(Cannot break)
If there are no surprises, the database and tables should be set up well.
An example program for how to store files
With this example, you can transfer files to the database via an HTML form.
Store.php3
Store.php3-by Florian Dittmer
?>
If you submit the form, the code is executed:
if ($submit) {
Connecting to a database
(You may need to adjust the hostname, 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)
[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 form that stores the new data is displayed
?>
}
?>
If you execute this program, you will see a simple HTML form, click Browse to select a file, and then click Submit.
When 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 use it later.
An example procedure for how to access a file
You can access the files you just saved through this program.
Getdata.php3-by Florian Dittmer
Calling 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 to access that file, you must use the ID as a parameter.
For example, a file has an ID of 2 in the database. You can call it this way:
getdata.php3?id=2
If you store the picture in a database, you can call it like a picture.
Example: A picture file has an ID of 3 in the database. You can call it this way:
How to store files larger than 1MB:
If you want to store files larger than 1MB, you have to make a lot of changes to your program, PHP settings, and SQL settings.
The following may help you to store files less than 24MB:
1, modify the STORE.PHP3, change the value of max_file_size to 24000000.
2, modify your PHP settings, in general, PHP only allow less than 2MB of files, you must change the value of Max_filesize (in php.ini) to 24000000
3, remove the MySQL packet size limit, in general, MySQL less than 1 MB of packets.
http://www.bkjia.com/PHPjc/364179.html www.bkjia.com true http://www.bkjia.com/PHPjc/364179.html techarticle if you want to store binary data, such as image files and HTML files, directly in your MySQL database, then this article is written for you! I will show you how to get through HTML tables ...