How to Use PHP + MYSQL to save and output files

Source: Internet
Author: User
Tags mysql query mysql connect

After a local file is uploaded to the server, the server script saves the file.
The file is saved to a specific directory on the machine, but there are many inconveniences such as file name duplication.
Automatically change the file name in sequence, and add the name to the upload time to ensure the uniqueness of the file name. In this way, the original file name is lost.
It is difficult to query specific file information by file name, which is not conducive to unified file management.
Save files to the database and use the powerful functions of the database to facilitate various file operations. This article uses
Two methods.

This groupProgramDemonstrate how to upload a file on the hard disk to the database on the server through a webpage, and
Read the content of a file.

Instructions for use:
A total of five programs are described as follows:
1. file. SQL --- structure of the database table used in this program [Note: the database uses test]
2. Upload. php --- upload form
3. Submit. php --- upload Handler
4. show_info.php --- display partial uploaded file information
5. show_add.php --- display the [Download] File

///////////////////////////////////// /// //
(1) file. SQL ---
// brief description
stores the database structure of the basic information of the uploaded file. Pay attention to the fields in the file content, use the longtext type
because the normal blob type can store a maximum of 64 KB. In addition, the maximum size of files to be uploaded is 2 MB by default in PHP. If the File Uploaded on
is too large, you cannot forget to adjust the setting of PHP. ini.
// File Source Code
Create Table receive (
ID int not null auto_increment, # primary key, automatically accumulate
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
upload interface. Select a file and submit it to submit. PHP processing
it is worth noting that a max_file_size hidden value field can be set to
limit the size of the uploaded file.
// program source code


File Upload form








method = 'post'>
Select Upload File
type = 'submit'>


//////////////////////////////////////// //////////////////////////////
(3) Submit. php ---
// Brief description
Save the basic information of uploaded files and files to the database.
// Program source code
<? PHP
If ($ myfile! = "NONE" & $ myfile! = "") {// Upload a file

// Set the time-out period. The default time is 30 seconds. If the value is set to 0, the time-out period is not limited.
$ Time_limit = 60;
Set_time_limit ($ time_limit );//

// Read the file content to the string
$ Fp = fopen ($ myfile, "rb ");
If (! $ FP) Die ("file open error ");
$ File_data = addslashes (fread ($ FP, filesize ($ myfile )));
Fclose ($ FP );
Unlink ($ myfile );

// File format, name, size
$ File_type = $ myfile_type;
$ File_name = $ myfile_name;
$ File_size = $ myfile_size;

// Connect to the database and save the file to the database
$ Conn = mysql_connect ("127.0.0.1 ","***","***");
If (! $ Conn) Die ("error: MySQL connect failed ");
Mysql_select_db ("test", $ conn );

$ SQL = "insert into receive
(File_data, file_type, file_name, file_size)
Values ('$ file_data', '$ file_type', '$ file_name', $ file_size )";
$ Result = mysql_query ($ SQL );

// The following sentence retrieves the ID of the insert statement.
$ Id = mysql_insert_id ();

Mysql_close ($ conn );

Set_time_limit (30); // restores the default timeout value.

Echo "Upload successful ---";
Echo "<a href = 'show _ INFO. php? Id = $ id'> display the information of the uploaded file </a> ";
}
Else {
Echo "You have not uploaded any files ";
}
?>

//////////////////////////////////////// //////////////////////////////
(4) show_info.php ---
// Brief description
Retrieve the basic information of the file [file name and file size] from the database.
// Program source code
<? PHP
If (! Isset ($ id) or $ id = "") Die ("error: Id none ");

// Location record, read
$ Conn = mysql_connect ("127.0.0.1 ","***","***");
If (! $ Conn) Die ("error: MySQL connect failed ");
Mysql_select_db ("test", $ conn );

$ SQL = "select file_name, file_size from receive where id = $ id ";
$ Result = mysql_query ($ SQL );
If (! $ Result) Die ("error: MySQL query ");

// If no specified record exists, an error is returned.
$ Num = mysql_num_rows ($ result );
If ($ num <1) Die ("error: No this recorder ");

// You can write the following two statements.
// $ ROW = mysql_fetch_object ($ result );
// $ Name = $ row-> name;
// $ Size = $ row-> size;
$ Name = mysql_result ($ result, 0, "file_name ");
$ Size = mysql_result ($ result, 0, "file_size ");

Mysql_close ($ conn );

Echo "<HR> information of the uploaded file :";
Echo "<br> the file's name-$ name ";
Echo "<br> the file's size-$ size ";
Echo "<br> <a href = show_add.php? Id = $ id> attachment </a> ";
?>

//////////////////////////////////////// //////////////////////////////
(5) show_add.php ---
// Brief description
Retrieve file content from the database
// Program source code
<? PHP
If (! Isset ($ id) or $ id = "") Die ("error: Id none ");

// Location record, read
$ Conn = mysql_connect ("127.0.0.1 ","***","***");
If (! $ Conn) Die ("error: MySQL connect failed ");
Mysql_select_db ("test", $ conn );
$ SQL = "select * From receive where id = $ id ";
$ Result = mysql_query ($ SQL );
If (! $ Result) Die ("error: MySQL query ");

$ Num = mysql_num_rows ($ result );
If ($ num <1) Die ("error: No this recorder ");

$ DATA = mysql_result ($ result, 0, "file_data ");
$ Type = mysql_result ($ result, 0, "file_type ");
$ Name = mysql_result ($ result, 0, "file_name ");

Mysql_close ($ conn );

// Output the corresponding file header and restore the original file name
Header ("Content-Type: $ type ");
Header ("content-Disposition: attachment; filename = $ name ");
Echo $ data;
?>

this program is passed in Win2000 Pro + Apache 1.13.19 + PhP 4.0.5 + MySQL 3.23.36.

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.