How to use PHPMYSQL to save and output files

Source: Internet
Author: User
Tags mysql connect
After a local file is uploaded to the server, the script on the server retains the file. There are two methods: one is to retain the file to a specific directory on the machine, however, there are many inconveniences such as file name duplication. some programs take the initiative to change the file name, add the name to the upload time, and other methods to ensure the uniqueness of the file name, in this way, the original name of the file is lost, and it is difficult to query the specific file information through the file name, which is not conducive to the same management of the file; one is

After a local file is uploaded to the server, the script on the server retains the file. There are two methods: one is to retain the file to a specific directory on the machine, however, there are many inconveniences such as file name duplication. some programs take the initiative to change the file name, add the name to the upload time, and other methods to ensure the uniqueness of the file name, in this way, the original name of the file is lost, and it is difficult to query the specific file information through the file name, which is not conducive to the same management of the file; one is to keep files in the database and use the powerful functions of the database to facilitate various file control. This document uses the second method.

This group of programs demonstrates how to upload a file on the hard disk to the database on the server through a webpage and read the file content.

Application clarification:

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
The database structure of the basic information of the uploaded file is retained. the fields of the file content are retained here. the longtext type can be used to store up to 64 K bytes of the normal blob type. In addition, the maximum size of the file to be uploaded is 2 MB by default in php. if the file to be uploaded is too large, you cannot forget to adjust the settings of php. ini.
// File Source code

Create table receive (
Id int not null auto_increment, # primary key, active 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
On the upload page, select a file and submit it to submit. php for 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
Keep the basic information of the uploaded files and files in the database.
// Program source code

If ($ myfile! = 'None' & $ myfile! = '') {// You have uploaded 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 pattern, 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 ('2017. 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 'display uploaded file information ';
}
Else {
Echo 'You have not uploaded any Files ';
}
?>


//////////////////////////////////////// //////////////////////////////

(4) show_info.php ---

// Brief description
Retrieve the basic file information [file name and file size] from the database.
// Program source code

If (! Isset ($ id) or $ id = '') die ('Error: id none ');
// Location record, read
$ Conn = mysql_connect ('2017. 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 'information of the uploaded File :';
Echo'
The file's name-$ name ';
Echo'
The file's size-$ size ';
Echo'
Attachment ';
?>

//////////////////////////////////////// //////////////////////////////

(5) show_add.php ---

// Brief description
Retrieve File content from the database
// Program source code

If (! Isset ($ id) or $ id = '') die ('Error: id none ');
// Location record, read
$ Conn = mysql_connect ('2017. 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 under win2000 pro apache 1.13.19 php 4.0.5 mysql 3.23.36.

 

 

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.