Teach you how to store pictures directly in the MySQL database

Source: Internet
Author: User
Tags html form sql mysql net query create database access mysql database

If you want to store binary data, such as picture files and HTML files, directly in your MySQL database, this article is written for you! I'll show you how to store these files through HTML forms, 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 (), Bin_data Longblob, fil Ename 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 is the referenced content:

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>

If you perform 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, and remember this ID, which will be used later.

An example of how to access a file:

You can access the files you just saved through this program.

The following are the referenced contents:

? Php

GetData. php3-by Florian Dittmer <dittmer@gmx.net>
Call method: GetData. Php3?id=<id>

if ($id) {

You may need to adjust the host name, 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 that to access that file, you must take the ID as an argument.

For example, the ID of a file in the database is 2. You can call it this way:

GetData. php3?id=2 If you store a picture in a database, you can call it as you would call a picture.

Example: The ID of a picture file in the database is 3, you can call it this way:

How to store files larger than 1MB:

If you want to store files larger than 1MB, you must make a number of changes to your program, PHP settings, and SQL settings.

The following may help you save files that are less than 24MB:

Modify the store. PHP3, change the value of max_file_size to 24000000.

To modify your PHP settings, in general, PHP only allows files less than 2MB, you must change the value of Max_filesize (in php.ini) to 24000000

Remove MySQL packet size limit, under normal circumstances MySQL less than 1 MB packet.

You must restart your MySQL with the following parameters

/usr/local/bin/safe_mysqld-o key_buffer=16m-o table_cache=128-o sort_buffer=4m-o record_buffer=1m-o max_allowed_ packet=24m



Related Article

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.