How to Use php + mysql to save and output files

Source: Internet
Author: User
Tags 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 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 for storing the basic information of the uploaded file. Pay attention to the fields for saving the file content here. 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 file is very large, and you have forgotten 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 hidden VALUE range of MAX_FILE_SIZE can be set
To limit the size of the uploaded file.
// Program source code
<Html>
<Head>
<Title> File Upload form </title>
</Head>
<Body>
<Table>
<Form enctype = 'multipart/form-data' name = 'myform' action = 'submit. php'
Method = 'post'>
<Input type = "hidden" NAME = "MAX_FILE_SIZE" VALUE = "1000000">
<Tr> <td> Select Upload File </td> <td>
<Input name = 'myfile' type = 'file'> </td> </tr>
<Tr> <td colspan = '2'> <input name = 'submit 'value = 'upload'
Type = 'submit '> </td> </tr>
</Table>
</Body>
</Html>

//////////////////////////////////////// //////////////////////////////
(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 "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 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.