How to use PHPMySQL to save and output files. After a local file is uploaded to the server, the script of the server saves the file in two ways. one is to save the file to a specific directory of the machine, however, after many local files are uploaded to the server, the server script saves the files. There are two methods:
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 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 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
The database structure that stores the basic information of the uploaded file. here, the field of the file content is saved, and the longtext type is used.
Because the blob type can store a maximum of 64 K bytes. In addition, the maximum size of files to be uploaded is 2 MB by default in php.
The uploaded files are incredibly large, so I don't forget to adjust the settings 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 for processing.
It is worth noting that a MAX_FILE_SIZE hidden VALUE can be
To limit the size of the uploaded file.
// Program source code
File upload form
Method = 'post'>
| Select Upload file |
|
| Type = 'submit '> |
The hosts file is saved to a specific directory on the machine, but there are many...