Home  >  Article  >  Backend Development  >  Implement PHP file upload module_PHP tutorial

Implement PHP file upload module_PHP tutorial

WBOY
WBOYOriginal
2016-07-14 10:10:06855browse

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