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

Jul 21, 2016 pm 04:06 PM
php+mysql generally superior keep use back and how right document server local of Script output conduct

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to create a script for editing? Tutorial on how to create a script through editing How to create a script for editing? Tutorial on how to create a script through editing Mar 13, 2024 pm 12:46 PM

Cutting is a video editing tool with comprehensive editing functions, support for variable speed, various filters and beauty effects, and rich music library resources. In this software, you can edit videos directly or create editing scripts, but how to do it? In this tutorial, the editor will introduce the method of editing and making scripts. Production method: 1. Click to open the editing software on your computer, then find the "Creation Script" option and click to open. 2. In the creation script page, enter the "script title", and then enter a brief introduction to the shooting content in the outline. 3. How can I see the "Storyboard Description" option in the outline?

How to save pictures without watermark in Xiaohongshu How to save pictures without watermark in Xiaohongshu How to save pictures without watermark in Xiaohongshu How to save pictures without watermark in Xiaohongshu Mar 22, 2024 pm 03:40 PM

Xiaohongshu has rich content that everyone can view freely here, so that you can use this software to relieve boredom every day and help yourself. In the process of using this software, you will sometimes see various beautiful things. Many people want to save pictures, but the saved pictures have watermarks, which is very influential. Everyone wants to know how to save pictures without watermarks here. The editor provides you with a method for those in need. Everyone can understand and use it immediately! 1. Click the &quot;...&quot; in the upper right corner of the picture to copy the link 2. Open the WeChat applet 3. Search the sweet potato library in the WeChat applet 4. Enter the sweet potato library and confirm to get the link 5. Get the picture and save it to the mobile phone album

What to do if the 0x80004005 error code appears. The editor will teach you how to solve the 0x80004005 error code. What to do if the 0x80004005 error code appears. The editor will teach you how to solve the 0x80004005 error code. Mar 21, 2024 pm 09:17 PM

When deleting or decompressing a folder on your computer, sometimes a prompt dialog box &quot;Error 0x80004005: Unspecified Error&quot; will pop up. How should you solve this situation? There are actually many reasons why the error code 0x80004005 is prompted, but most of them are caused by viruses. We can re-register the dll to solve the problem. Below, the editor will explain to you the experience of handling the 0x80004005 error code. Some users are prompted with error code 0X80004005 when using their computers. The 0x80004005 error is mainly caused by the computer not correctly registering certain dynamic link library files, or by a firewall that does not allow HTTPS connections between the computer and the Internet. So how about

How to transfer files from Quark Cloud Disk to Baidu Cloud Disk? How to transfer files from Quark Cloud Disk to Baidu Cloud Disk? Mar 14, 2024 pm 02:07 PM

Quark Netdisk and Baidu Netdisk are currently the most commonly used Netdisk software for storing files. If you want to save the files in Quark Netdisk to Baidu Netdisk, how do you do it? In this issue, the editor has compiled the tutorial steps for transferring files from Quark Network Disk computer to Baidu Network Disk. Let’s take a look at how to operate it. How to save Quark network disk files to Baidu network disk? To transfer files from Quark Network Disk to Baidu Network Disk, you first need to download the required files from Quark Network Disk, then select the target folder in the Baidu Network Disk client and open it. Then, drag and drop the files downloaded from Quark Cloud Disk into the folder opened by the Baidu Cloud Disk client, or use the upload function to add the files to Baidu Cloud Disk. Make sure to check whether the file was successfully transferred in Baidu Cloud Disk after the upload is completed. That's it

How to execute .sh file in Linux system? How to execute .sh file in Linux system? Mar 14, 2024 pm 06:42 PM

How to execute .sh file in Linux system? In Linux systems, a .sh file is a file called a Shell script, which is used to execute a series of commands. Executing .sh files is a very common operation. This article will introduce how to execute .sh files in Linux systems and provide specific code examples. Method 1: Use an absolute path to execute a .sh file. To execute a .sh file in a Linux system, you can use an absolute path to specify the location of the file. The following are the specific steps: Open the terminal

What is hiberfil.sys file? Can hiberfil.sys be deleted? What is hiberfil.sys file? Can hiberfil.sys be deleted? Mar 15, 2024 am 09:49 AM

Recently, many netizens have asked the editor, what is the file hiberfil.sys? Can hiberfil.sys take up a lot of C drive space and be deleted? The editor can tell you that the hiberfil.sys file can be deleted. Let’s take a look at the details below. hiberfil.sys is a hidden file in the Windows system and also a system hibernation file. It is usually stored in the root directory of the C drive, and its size is equivalent to the size of the system's installed memory. This file is used when the computer is hibernated and contains the memory data of the current system so that it can be quickly restored to the previous state during recovery. Since its size is equal to the memory capacity, it may take up a larger amount of hard drive space. hiber

How to download and save Douyin videos How to download and save Douyin videos Mar 29, 2024 pm 02:16 PM

1. Open the Douyin app, find the video you want to download and save, and click the [Share] button in the lower right corner. 2. In the pop-up window that appears, slide the function buttons in the second row to the right, find and click [Save Local]. 3. A new pop-up window will appear at this time, and the user can see the download progress of the video and wait for the download to complete. 4. After the download is completed, there will be a prompt of [Saved, please go to the album to view], so that the video just downloaded will be successfully saved to the user's mobile phone album.

How to configure Dnsmasq as a DHCP relay server How to configure Dnsmasq as a DHCP relay server Mar 21, 2024 am 08:50 AM

The role of a DHCP relay is to forward received DHCP packets to another DHCP server on the network, even if the two servers are on different subnets. By using a DHCP relay, you can deploy a centralized DHCP server in the network center and use it to dynamically assign IP addresses to all network subnets/VLANs. Dnsmasq is a commonly used DNS and DHCP protocol server that can be configured as a DHCP relay server to help manage dynamic host configurations in the network. In this article, we will show you how to configure dnsmasq as a DHCP relay server. Content Topics: Network Topology Configuring Static IP Addresses on a DHCP Relay D on a Centralized DHCP Server

See all articles