Home >Backend Development >PHP Tutorial >How to save and output files using PHP MySQL_PHP Tutorial
After the local file is uploaded to the server, the server script saves the file. There are generally two ways, one is as
The file is saved to a specific directory on the machine, but there are many inconveniences caused by duplicate file names. Some procedures
The program automatically changes the file name, adds the upload time to the name, etc. to ensure the uniqueness of the file name, thus losing the originality of the file
There are also many difficulties in querying specific file information through file names, which is not conducive to unified management of files; one is to use the file
Save the file to the database and use the powerful functions of the database to conveniently implement various operations on the file. This article uses the
Two methods.
This set of programs demonstrates how to upload a file from the hard disk to the server's database through a web page, and
Read the contents of the file.
Instructions for use:
There are 5 programs in total, the descriptions are as follows:
1. file.sql --- The structure of the database table used in this program [Note: the database used is test]
2. upload.php --- upload form
3. submit.php --- upload handler
4. show_info.php --- Display part of the uploaded file information
5. show_add.php --- show [download] file
////////////////////////////////////////////////////// /////////////////////
(1) file.sql ---
//Brief description
The database structure that saves the basic information of the uploaded file. Here we focus on the fields that save the file content, using the longtext type
Because the ordinary blob type can store up to 64K bytes. In addition, the default configuration of general php has a maximum upload file of 2M. If the above is
The uploaded file is very large, so don’t forget to adjust the settings of php.ini.
//File source code
create table receive(
id int 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
In the upload interface, the user selects the file and then submits it to submit.php for processing
It is worth noting that there is a hidden value field of MAX_FILE_SIZE, which can be set by setting its VALUE
to limit the size of uploaded files.
//Program source code
Select upload file | |
type='submit'> |