Home php教程 php手册 如何利用php+mysql保存和输出文件

如何利用php+mysql保存和输出文件

Jun 13, 2016 pm 12:42 PM
php+mysql generally superior keep use back and how right document server local of Script output conduct

本地文件上传到服务器后,服务器的脚本对文件进行保存,一般有两种方式,一种是作为
文件保存到机器的特定目录下,但是这里就有很多诸如文件重名带来的种种不便之处,有的程
序自动改文件名字,把名字加上上传时间等方法以保证文件名的唯一性,这样失去了文件的原
始名字,通过文件名查询特定的文件信息也有很多困难,不利于文件的统一管理;一种是把文
件保存到数据库中利用数据库的强大功能,可以方便的实现文件的各种操作。本文采用的是第
二种方法。

    这一组程序演示了,如何将硬盘的一个文件通过网页,上传到服务器的数据库里面,并且
读出文件的内容。

使用说明:
一共有5个程序,说明如下:
1. file.sql      --- 本程序要用到的数据库表的结构[注:数据库用的是test]
2. upload.php    --- 上传表单
3. submit.php    --- 上传处理程序
4. show_info.php --- 显示部分上传的文件信息
5. show_add.php  --- 显示[下载]文件

//////////////////////////////////////////////////////////////////////
(1)file.sql ---
//简要说明
保存上传得文件的基本信息的数据库结构,此处注意保存文件内容的字段,使用longtext类型
因为普通的blob类型最大存储64K字节。另外,一般php的默认配置最大上传文件为2M,如果上
传的文件特别大,莫忘了调整php.ini的设置哦。
//文件源码
create table receive(
    id int NOT NULL auto_increment, #主键,自动累加
    file_data longblob,             #文件内容
    file_type varchar(100),         #文件类型
    file_name varchar(255),         #文件名字  
    file_size int,                  #文件大小
    PRIMARY KEY(id) #主键
)

//////////////////////////////////////////////////////////////////////
(2)upload.php ---
//简要说明
上传界面,用户选择文件,然后提交给submit.php处理
值得注意的是一个 MAX_FILE_SIZE的隐藏值域,通过设置其VALUE可  
以限制上载文件的大小。
//程序源码
   

   
文件上传表单   
   
   
   
method='post'>   

  
   
选择上传文件
type='submit'>
   
   


//////////////////////////////////////////////////////////////////////
(3)submit.php ---
//简要说明
把用户上传得文件连同文件的基本信息保存到数据库里
//程序源码
    if($myfile != "none" && $myfile != "") { //有了上传文件了  

        //设置超时限制时间,缺省时间为 30秒,设置为0时为不限时
        $time_limit=60;          
        set_time_limit($time_limit); //

        //把文件内容读到字符串中
        $fp=fopen($myfile,  "rb");
        if(!$fp) die("file open error");
        $file_data = addslashes(fread($fp, filesize($myfile)));
        fclose($fp);
        unlink($myfile);  

        //文件格式,名字,大小
        $file_type=$myfile_type;
        $file_name=$myfile_name;
        $file_size=$myfile_size;

        //连接数据库,把文件存到数据库中
        $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);

        //下面这句取出了刚才的insert语句的id
        $id=mysql_insert_id();

        mysql_close($conn);

        set_time_limit(30); //恢复缺省超时设置  

        echo "上传成功--- ";
        echo "显示上传文件信息";
    }   
    else {   
        echo "你没有上传任何文件";   
    }   
?>  

//////////////////////////////////////////////////////////////////////
(4)show_info.php ---
//简要说明
从数据库里取出文件的基本信息[文件名和文件大小]。
//程序源码
    if(!isset($id) or $id=="") die("error: id none");

    //定位记录,读出
    $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");

    //如果没有指定的记录,则报错
    $num=mysql_num_rows($result);
    if($num
    //下面两句程序也可以这么写
    //$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 "
The file's name - $name";   
    echo "
The file's size - $size";  
    echo "
附件";
?>

//////////////////////////////////////////////////////////////////////
(5)show_add.php ---
//简要说明
从数据库里取出文件内容
//程序源码
    if(!isset($id) or $id=="") die("error: id none");

    //定位记录,读出
    $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
    $data = mysql_result($result,0,"file_data");
    $type = mysql_result($result,0,"file_type");
    $name = mysql_result($result,0,"file_name");

    mysql_close($conn);

    //先输出相应的文件头,并且恢复原来的文件名
    header("Content-type:$type");
    header("Content-Disposition: attachment; filename=$name");
    echo $data;
?>

本程序在 win2000 pro + apache 1.13.19 + php 4.0.5 + mysql 3.23.36 下通过。 
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 "..." 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 "Error 0x80004005: Unspecified Error" 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