Home > Backend Development > PHP Tutorial > How to use php+mysql to save and output files_PHP tutorial

How to use php+mysql to save and output files_PHP tutorial

WBOY
Release: 2016-07-21 16:06:44
Original
1019 people have browsed it

After the local file is uploaded to the server, the server script saves the file. There are generally two ways. One is to save it as a
file in a specific directory of the machine. However, there are many problems such as duplicate file names. The inconvenience is that some programs automatically change the name of the file and add the upload time to the name to ensure the uniqueness of the file name. In this way, the original name of the file is lost, and specific files are queried through the file name. There are also many difficulties in file information, which is not conducive to the unified management of files; one is to save the files in the database and use the powerful functions of the database to conveniently implement various operations on the files. This article uses the second method
.

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, and the instructions are as follows:
1. file.sql --- The structure of the database table to be 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. Pay attention here to the field that saves the file content. Use the longtext type
because the ordinary blob type can store up to 64K bytes. In addition, generally the default configuration of PHP has a maximum upload file of 2M. If the uploaded file is particularly large, 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,  A> File_name varchar (255),#file name
file_size int,#file size
Primary Key (ID)#Primary key

/////////// ////////////////////////////////////////////////////// //////////
(2) upload.php ---
//Brief description
Upload interface, the user selects the file and then submits it to submit.php for processing
Worth it Note that there is a hidden value field of MAX_FILE_SIZE. By setting its VALUE, you can
limit the size of the uploaded file.
//Program source code


File upload form

;

method='post'>
< INPUT TYPE = "hidden" NAME = "MAX_FILE_SIZE" VALUE ="1000000">


Select upload file
type='submit'>

////////////////////////////////////////////////////// ////////////////////
(3) submit.php ---
//Brief description
Put the file uploaded by the user together with the basic information of the file The information is saved in the database
//Program source code
if($myfile != "none" && $myfile != "") { //There is an uploaded file

                                                                                                                                                                              ​/Read the file content 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 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 takes out the id of the insert statement just now
$id=mysql_insert_id();

mysql_close ($ conn);

set_time_limit(30); //Restore the default timeout setting

echo "Upload successful --- ";
echo "Show uploaded file information";
}  
else {  
 echo "You did not upload any files";  
}  
?>

////////////////////////////////////////////// ////////////////////////////
(4) show_info.php ---
//Brief description
From the database Get the basic information of the file [file name and file size].
//Program source code
$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 there is no specified record, an error will be reported
$num=mysql_num_rows($result);
if($num<1) die("error: no this recorder");

//The following two programs can also be written like this
//$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 "
Uploaded file information:";
echo "
The file's name - $name";
echo "
The file's size - $size";
echo "
Attachment";
?>

/////////////////////////// //////////////////////////////////////////////
(5) show_add.php ---
//Brief description
Retrieve the file content from the database
//Program source code

//Locate the record, 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 program passed under win2000 pro + apache 1.13.19 + php 4.0.5 + mysql 3.23.36.​



http://www.bkjia.com/PHPjc/315293.html

www.bkjia.com

http: //www.bkjia.com/PHPjc/315293.htmlTechArticleAfter the local file is uploaded to the server, the server's script saves the file. There are generally two ways, one is Save as a file to a specific directory on the machine, but there are many here...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template