How to save and output files using Php+mysql _php tutorial

Source: Internet
Author: User
Tags mysql connect
After the local file is uploaded to the server, the server's script saves the file, usually in two ways, one of which is as
The file is saved to a specific directory of the machine, but there are many inconveniences, such as the name of the file,
Automatically change the name of the file, add the name of the upload time and other methods to ensure the uniqueness of the file name, so lost the original
First name, through the file name to query the specific file information also have many difficulties, not conducive to the unified management of documents;
Save to the database to take advantage of the powerful features of the database, you can easily implement various operations of the file. This article uses the first
Two different methods.

This set of programs demonstrates how to upload a file of a hard disk through a Web page to the server's database, and
read out the contents of the file.

Instructions for use:
A total of 5 procedures, described below:
1. File.sql---The structure of the database tables to be used in this program [Note: Database is test]
2. upload.php---upload form
3. submit.php---Upload handler
4. show_info.php---Display some of the 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 the fields that hold the contents of the file are stored, using the Longtext type
Because the normal BLOB type has a maximum storage of 64K bytes. In addition, general PHP default configuration maximum upload file is 2M, if the
The file is particularly large, do not forget to adjust the php.ini settings Oh.
File source
CREATE TABLE Receive (
ID int not NULL auto_increment, #主键, auto-accumulate
File_data Longblob, #文件内容
File_type varchar (+), #文件类型
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
It is worth noting that a max_file_size's hidden range is set by setting its value to
To limit the size of the uploaded file.
Program source Code


<title>File Upload Form</title>








Method= ' Post ' >
Select Upload File

Type= ' Submit ' >




//////////////////////////////////////////////////////////////////////
(3) submit.php---
Brief description
Save the user's uploaded files along with the file's basic information in the database
Program source Code
if ($myfile! = "None" && $myfile! = "") {//have uploaded file

Set timeout limit time, default time is 30 seconds, set to 0 for unlimited
$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 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 default timeout settings

echo "Upload successful---";
echo "Show upload file Information";
}
else {
echo "You didn't upload any files";
}
?>

//////////////////////////////////////////////////////////////////////
(4) show_info.php---
Brief description
Basic information [file name and file size] for extracting files from the database.
Program source Code
if (!isset ($id) or $id = = "") Die ("Error:id none");

Locate 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, an error
$num =mysql_num_rows ($result);
if ($num <1) die ("Error:no this recorder");

The following two sentences can be written as well.
$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 "
Annex ";
?>

//////////////////////////////////////////////////////////////////////
(5) show_add.php---
Brief description
Extracting the contents of a file from a database
Program source Code
if (!isset ($id) or $id = = "") Die ("Error:id none");

Locate records, 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 filename
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.

http://www.bkjia.com/PHPjc/315293.html www.bkjia.com true http://www.bkjia.com/PHPjc/315293.html techarticle after the local file is uploaded to the server, the server's script saves the file in two ways, one of which is saved to the machine's specific directory as a file, but there are many ...

  • 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.