search
Homephp教程php手册实现PHP文件上传模块

实现PHP文件上传模块

Jun 13, 2016 am 10:56 AM
lampphpuploadaccomplishsuccessdocumentmoduleenvironmentmake surewantConfigurationfirst

首先确保LAMP已经配置成功。

环境:Opensuse12.2 LAMP

今天要实现的是php的文件上传模块,以上传图片和pdf文档为例。

步骤如下:

修改php.ini

linux下的php.ini放在了/etc/php5/apache2文件夹下,终端运行

[plain]
sudo vim /etc/php5/apache2/php.ini 

sudo vim /etc/php5/apache2/php.ini


用“/×××”在vim中查找下面的值,进行修改:

upload_max_filesize = 10M


该选项表示上传文件的最大字节长度。默认2M,改为10M

post_max_size = 12M

 该选项代表允许POST数据的最大字节长度,默认是8M,建议设定值比upload_max_filesize略大。


memory_limit默认128M,如果要上传的文件大小超过这个值,就需要修改,这里不需要修改。

修改完之后保存。

 


创建项目

在/srv/www/htdocs/创建test2文件夹,里面创建两个文件:upload.html,upload_file.php.还要创建一个upload文件夹用于存放上传的文件。

首先对upload文件夹处理一下,修改它的权限。

终端运行:


[plain]
sudo chmod 777 upload -R 

sudo chmod 777 upload -R

 

下面是代码清单。

upload.html

 

[html]
 

 
 
enctype="multipart/form-data"> 
 
  

 
 
 
 
 
 


enctype="multipart/form-data">







upload_file.php


[php]
 
if(($_FILES["file"]["type"] == "image/png")||($_FILES["file"]["type"] == "application/pdf")) 
  { 
  if ($_FILES["file"]["error"] > 0) 
    { 
    echo "Return Code: " . $_FILES["file"]["error"] . "
"; 
    } 
  else 
    { 
    echo "Upload: " . $_FILES["file"]["name"] . "
"; 
    echo "Type: " . $_FILES["file"]["type"] . "
"; 
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
"; 
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "
"; 
 
    if (file_exists("upload/" . $_FILES["file"]["name"])) 
      { 
      echo $_FILES["file"]["name"] . " already exists. "; 
      } 
    else 
      { 
      move_uploaded_file($_FILES["file"]["tmp_name"], 
      "upload/" . $_FILES["file"]["name"]); 
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; 
      } 
    } 
  } 
else 
  { 
  echo "Invalid file"; 
  } 
?> 

if(($_FILES["file"]["type"] == "image/png")||($_FILES["file"]["type"] == "application/pdf"))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "
";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "
";
    echo "Type: " . $_FILES["file"]["type"] . "
";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "
";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>
简单解释一下。

html文件中就是一个form,用来处理与用户的交互,注意按钮的属性。运行起来是这样的

 

 

 

选择好文件,点击Submit之后,后台的处理就交给php了。

 


php通过全局数组_FILE获取文件的一些属性,然后对其进行相应的处理。

$_FILES["file"]["error"] 中放的是出错代码,对应的错误如下:


编码
 值
 说明
 
UPLOAD_ERR_OK
 0
 文件成功上传
 
UPLOAD_ERR_INI_SIZE
 1
 文件大小比php.ini中upload_max_filesize指定值要大
 
UPLOAD_ERR_FORM_SIZE
 2
 文件的 小比表单的MAX_FILE_SIZE指定的值大
 
UPLOAD_ERR_PARTIAL
 3
 文件上传不完整(可能因为请求时间过长被终止)
 
UPLOAD_ERR_NO_FILE
 4
 没有文件随着这个请求上传
 
UPLOAD_ERR_NO_TMP_DIR
 6
 在php.ini中没有指定临时文件夹


 

 

文件上传好之后就可以在upload中看到上传好的文件了。

 

 

 

做到这里已经差不多了,但还可以扩展,比如上传大文件时显示进度条,比如上传文件同时写入数据库,然后在页面中显示文件名,点击可以下载。

时间关系,就到这里。

 

Statement
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)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools