Local files uploaded to the server, the server's script to save the file, generally there are two ways, one is saved as a file to the machine in a specific directory, but there are many such as the file name of the various inconvenience caused, and some programs automatically change file names, Add the name of the upload time to ensure the uniqueness of the file name, this lost the original name of the file, through the file name query specific file information also has a lot of difficulties, is not conducive to the unified management of documents; one is to save the file to the database to use the powerful functions of the database, you can easily implement a variety of file operations. The second method is used in this paper.
This set of programs demonstrates how to upload a file from a hard disk to the server's database and read the contents of the file.
Instructions for use:
A total of 5 procedures are described below:
1. File.sql---The structure of the database table to be used by this program [NOTE: The database is using test]
2. upload.php---Upload the form
3. submit.php---Upload handler
4. show_info.php---Display part of uploaded file information
5. show_add.php---Display [download] File
///////////////////////////////////////////////////
(1) File.sql---
Brief description
The database structure that holds the basic information of the uploaded file, where you note the fields where the contents of the file are saved, using the Longtext type to store the maximum 64K bytes for the normal blob type. In addition, the general PHP default configuration maximum upload file for 2M, if the file upload is particularly large, do not forget to adjust the php.ini settings Oh.
File source
CREATE TABLE Receive (
ID int not NULL auto_increment, #主键, automatic accumulation
File_data Longblob, #文件内容
File_type varchar (MB), #文件类型
file_name varchar (255), #文件名字
file_size int, #文件大小
PRIMARY KEY (ID) #主键
)
//////////////////////////////////////////////////////////////////////
(2) upload.php---
Brief description
Upload interface, user select File, then submit to submit.php processing noteworthy is a max_file_size hidden range, by setting its value to limit the size of the uploaded file.
Program source Code
<title> File Upload Form </title>
<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>
//////////////////////////////////////////////////////////////////////
(3) submit.php---
Brief description
Save the user upload file with the basic information of the file in the database
Program source Code
<?php
if ($myfile!= "None" && $myfile!= "") {//with upload file
Set timeout limit time, default time is 30 seconds, set to 0 is not limited
$time _limit=60;
Set_time_limit ($time _limit); //
Read the contents of the file into a 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 the database, 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 takes out the ID of the INSERT statement just now.
$id =mysql_insert_id ();
Mysql_close ($conn);
Set_time_limit (30); Restore default timeout settings
echo "Upload success---";
echo "<a href= ' show_info.php?id= $id ' > show uploaded file information </a>";
}
else {
echo "You didn't upload any files";
}
?>
//////////////////////////////////////////////////////////////////////
(4) show_info.php---
Brief description
Extract the basic information about the file from the database [file name and file size].
Program source Code
<?php
if (!isset ($id) or $id = = "") Die ("Error:id none");
//Positioning records, read out
$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 record is specified, the error
$num =mysql_num_rows ($result);
if ($num <1) die ("Error:no this recorder");
//The following two procedures can also be written as follows
//$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 > Accessories </a>";
?>
//////////////////////////////////////////////////////////////////////
(5) show_add.php---
Brief description
Extract the contents of a file from a database
Program source Code
<?php
if (!isset ($id) or $id = = "") Die ("Error:id none");
Locate the record and read out
$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);
First output the corresponding file header, and restore the original file name
Header ("Content-type: $type");
Header ("content-disposition:attachment; Filename= $name ");
Echo $data;
?>
This procedure is passed under Win2000 Pro Apache 1.13.19 php 4.0.5 MySQL 3.23.36.