How to store pictures directly in MySQL

Source: Internet
Author: User
Tags html form create database mysql database


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>




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.