How to store music and picture files in mysql

Binary data , such as image files and HTML files , stored directly in the MySQL database
how through HTML to store these files forms, how to access and use these files.
This paper outlines: 

In mysql to create a new database in
a file stored in an example of how to program
an example of how to access a program file
 

In mysql establishment of a new database
must first be in mysql to create a new database, will those binary files stored in this database. Will be used in the following example structure, in order to establish the database, you must do the following steps:
entering 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,
the Description CHAR (50), bin_data LONGBLOB, filename CHAR (50), filesize CHAR (50), filetype CHAR (50)); " (not broken line)
if there is no accident, databases and tables should be established as well.
Examples of how a program stored in a file with this example you can Html transfer files to the database form.

Store.php3

<?php

// store.php3 - by Florian Dittmer <[email protected]>;

?>;

 

<HTML>

<HEAD><TITLE>Store binary data into SQL Database</TITLE></HEAD>

<BODY>

 

<?php

// If you submit the form, the code will be executed :

if ($submit) {

     // connect to the database

     // ( You may need to adjust the host name, user name 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 following Database ID: $id”;
MYSQL_CLOSE();
} else {
// 否则显示储存新数据的表单
?>;

<form method="post" action=" <?php echo $PHP_SELF; ?>" enctype="multipart/form-data">

    File Description:<br>

    <input type="text" name="form_description"  size="40">

    <INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="1000000">

    <br>;File to upload/store in database:<br>

    <input type="file" name="form_data"  size="40">

    <p>;<input type="submit" name="submit" value="submit">

    </form>

<?php
}
?>

</BODY>

</ HTML>

If this program is executed, will see a simple Html form, click " Browse " to select a file, then click Submit.  
When the file is uploaded to the web after the server, the program will tell just upload the file ID , remember this ID , will be of use.
An example of how to access the program files 
can be accessed through this process you have just saved the file 
<PHP?

// getdata.php3 - by Florian Dittmer <[email protected]>

// call the method :? Getdata.php3 id = <id>


IF ($ the above mentioned id) {
// you may need to adjust the host name, user name and password :

@MYSQL_CONNECT ( "localhost", "root", "password");
@mysql_select_db ( "binary_data");
$ Query = "the SELECT bin_data, from binary_data WHERE ID = filetype ID $ ";
$ @MYSQL_QUERY Result = ($ Query);
$ @MYSQL_RESULT Data = (Result $, 0," bin_data ");
$ = @MYSQL_RESULT type ($ Result, 0," filetype " );

Header ( "Content-of the type: $ of the type");
echo $ the Data;

}
?>

 

To access the program must know that document, you have to ID as a parameter. 
For example: a file ID in the database for 2. You can call it like this: 
    ? Getdata.php3 the above mentioned id = 2

If you save the picture in the database, you can call it as to call the picture.
Example: a picture ID in the database file is 3. You can call it like this: 
     <img src = "? Getdata.php3 the above mentioned id = 3">; 
how to store files larger than 1MB of: 
if you want to store files larger than 1MB, and you have to your program, PHP settings, SQL settings many changes.
The following few may be able to help you to store files smaller than 24MB of: 
modifying store.php3, the value MAX_FILE_SIZE changed to 24 million. 
Modify your PHP provided, in general, less than 2MB PHP only files, you must max_filesize value (in php.ini) was changed to 24 million

removed MYSQL packet size restrictions, in general less than 1 MYSQL MB data package. 
you have to 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
If you are still wrong :
it may be a timeout error, if you are very slow to store a large file through a connection, PHP default time limit is 30 seconds.
You can max_execution_time value (in php.ini) was changed to -1

 

How to use php + mysql and save the output files
after local files uploaded to the server, the server script to save the file, there are two ways, one is as a 
file is saved to the machine under a specific directory, but here there are many such files of the same name at the inconvenience brought about by some process 
sequence automatically change the file name, plus the name of the upload time and other methods to ensure the uniqueness of this lost file the original file name 
beginning name, by file name specific query file information there are many difficulties, is not conducive to the unified management of documents; one is the text of 
the holding member to the database using the power of the database, you can easily achieve a variety of file operations. Employed herein are 
two kinds of methods. 
    This group program demonstrates how the hard disk by a Web page file, uploaded to the database server inside and 
read the contents of the file. 

Description: 
There are five programs, as follows: 
the structure of the Program 1. file.sql --- to use database table [Note: The database is used Test] 
2. upload form, upload.php, --- 
3 . submit.php --- upload handler 
4. show_info.php --- uploaded file information display section 
5. show_add.php --- display [Download] file 

////////////////////////////////////////////////// //////////////////// 
(1) file.sql --- 
// brief description of the 
structure of the database of basic information was uploaded to save the file, save the file contents note here fields, using longtext type 
because the average maximum storage 64K bytes blob types. In addition, the general default maximum upload php configuration file to 2M, if the 
transfer of the file is big, not forget the oh adjust php.ini settings. 
// File source 
Create Table the receive ( 
    ID int the NOT NULL AUTO_INCREMENT, # primary key, automatic accumulation 
    file_data longblob, # file content 
    file_type varchar (100), # file type 
    file_name varchar (255), # file name  
    file_size int, # file size 
    PRIMARY kEY (id) # primary key 

////////////////////////////////////////////////// //////////////////// 
(2) upload.php --- 
// brief description of the 
upload interface, the user selects a file, and then submitted to the submit.php deal 
is worth noting a range MAX_FILE_SIZE is hidden by setting its VALUE may be  
to limit the size of the file. 
// Open Source 
<HTML>   
<head>   
<title> file upload form </ title>   
</ head>   
<body>   
<Table>   
<form the enctype = 'multipart / form-Data' name = 'MyForm' Action = 'Submit .php '  
Method =' POST '>   
<the INPUT the TYPE = "hidden" nAME = "The MAX_FILE_SIZE" of VALUE = "1000000"> 
<TR> <TD> to select the file upload </ TD> <TD> 
<INPUT name =' myfile ' type = 'file'> </   td>


</table>   
</body>   
</html>

 

 

Guess you like

Origin blog.csdn.net/mysteryflower/article/details/93723238