If you want to put binary data, such as picture files and HTML files, directly stored 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 of how to save a file
An example of how to access a file program
Create 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 build the database, you have to do the following steps:
Access to the MySQL controller
Enter command "CREATE database binary_data;"
Enter the command "use Binary_data;"
Enter command "CREATE TABLE binary_data (ID INT (4) Not NULL auto_increment PRIMARY KEY.
Description char (m), Bin_data longblob, filename char (m), FileSize char (50), filetype char (+));
If there are no surprises, the database and tables should be set up.
An example of how to store files with this example you can transfer files to a database through an HTML form.
Store.php3
The following are the referenced contents:
<?php
store.php3-by Florian Dittmer <dittmer@gmx.net>
?>
<HTML>
<BODY>
<?php
If a form is submitted, the code is executed:
if ($submit) {
Connecting to a 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 "<p>this file has the following Database ID: <b> $id </b>";
Mysql_close ();
} else {
Otherwise display the form that stores the new data
?>
<form method= "POST" action= "<?php echo $PHP _self;?>" enctype= "Multipart/form-data" >
File description:<br>
<input type= "text" name= "form_description" size= ">"
<input type= "hidden" name= "max_file_size" value= "1000000" >
<br>file to Upload/store in database:<br>
<input type= "File" Name= "Form_data" size= ">"
<p><input type= "Submit" name= "submit" value= "Submit" >
</form>
<?php
}
?>
</BODY>
</HTML>