Home Backend Development PHP Tutorial Implement PHP file upload module_PHP tutorial

Implement PHP file upload module_PHP tutorial

Jul 14, 2016 am 10:10 AM
lamp php upload accomplish success document module environment make sure want Configuration first

First make sure LAMP has been configured successfully.

Environment: Opensuse12.2 LAMP

What we are going to implement today is the file upload module of PHP, taking uploading pictures and PDF documents as an example.

The steps are as follows:

Modify php.ini

The php.ini under Linux is placed in the /etc/php5/apache2 folder, and the terminal is run

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

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


Use "/×××" to find the following value in vim and modify it:

upload_max_filesize = 10M


This option indicates the maximum byte length of the uploaded file. Default 2M, changed to 10M

post_max_size = 12M

This option represents the maximum byte length allowed for POST data. The default is 8M. It is recommended to set the value slightly larger than upload_max_filesize.


The default memory_limit is 128M. If the size of the file to be uploaded exceeds this value, it needs to be modified. No modification is required here.

Save after modification.


Create project

Create the test2 folder in /srv/www/htdocs/, and create two files in it: upload.html, upload_file.php. Also create an upload folder to store the uploaded files.

First deal with the upload folder and modify its permissions.

Terminal run:


[plain]
sudo chmod 777 upload -R

sudo chmod 777 upload -R

The following is the code listing.

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中看到上传好的文件了。

 

 

 

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

时间关系,就到这里。

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/477552.htmlTechArticle首先确保LAMP已经配置成功。 环境:Opensuse12.2 LAMP 今天要实现的是php的文件上传模块,以上传图片和pdf文档为例。 步骤如下: 修改php.ini...
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 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

See all articles