Home  >  Article  >  Backend Development  >  What is the php file upload process?

What is the php file upload process?

coldplay.xixi
coldplay.xixiOriginal
2021-02-25 16:40:563141browse

php file upload process: first configure the PHP file and set the front-end form; then save the temporary file in the set directory by default; then judge and process the error attribute in the file array; finally, in the front-end you can Limit the type and size of uploaded files.

What is the php file upload process?

The operating environment of this tutorial: Windows 7 system, PHP version 5.6, DELL G3 computer. This method is suitable for all brands of computers.

php file upload process:

1. PHP configuration file

The file uploads section in the php.ini file defines the relevant file upload configuration.

  • file_uploads = On Turn on file uploads

  • upload_tmp_dir =Temporary directory

  • upload_max_filesize = 2M maximum Upload size

  • max_file_uploads = 20 Maximum number of files uploaded at a time

2. Front-end form

<form action="test.php" method="post" enctype="multipart/form-data">
  <input type="file" name="myfile">
  <input type="submit" value="上传">
</form>

When using the file When uploading the form of the control, the enctype of the form must be set to "multipart/form-data".

You can limit the size and type of uploaded files on the client side:

<input type="hidden" name="MAX_FILE_SIZE" value="8096">
<input type="file" name="myfile" accept="image/jpeg,image/png">

3. Background processing

After uploading, you will get a $_FILES on the server side The file array is as follows:

What is the php file upload process?

The temporary files uploaded to the server are saved by default in the directory set by upload_tmp_dir in php.ini, we need Move the file to the specified location:

<?php
  header(&#39;Content-type:text/html;charset=utf-8&#39;);
  $myfile=$_FILES[&#39;myfile&#39;];
  //获取文件数组[&#39;myfile&#39;]的属性tmp_name与name
  $tmp=$myfile[&#39;tmp_name&#39;];
  $picname=$myfile[&#39;name&#39;];
  //windows采用GBK编码,php采用utf-8编码,需要改变文件名的编码格式
  $picname=iconv(&#39;utf-8&#39;,&#39;GBK&#39;,$picname);
  //将零时文件$tmp移动到指定目录userpic并命名为$picname
  copy($tmp,"userpic/".$picname);

4. Error handling

Judge and process the error attribute in the file array:

$error=$myfile[&#39;myfile&#39;][&#39;error&#39;];
  if ($error==0){//为0,代表上传成功
    if (move_uploaded_file($tmp,"userpic/".$picname)) {
      echo "移动文件成功";
    }else{
      echo "移动文件失败";
    }
  }else{//不为0,匹配错误信息
    switch ($error){
      case 1:
        echo "超过文件最大上传限制";//php.ini中upload_max_filesize
        break;
      case 2:
        echo "超过表单文件大小限制";//HTML表单中MAX_FILE_SIZE选项
        break;
      case 3:
        echo "文件部分被上传";
        break;
      case 4:
        echo "未选择上传文件";
        break;
      case 7://文件写入失败
      case 8:
        echo "系统错误";
        break;
    }
  }

5. Upload restrictions

On the front end, you can limit the type and size of uploaded files:

<!--表单隐藏域对上传大小进行限制-->
<input type="hidden" name="MAX_FILE_SIZE" value="2097152">
<!--accept 对文件类型限制-->
<input type="file" name="myfile" accept="image/jpeg,image/png">

Note that the limit is in bytes

There are also response restrictions on the server side:

$size=$myfile[&#39;size&#39;];//获取文件大小
$MaxSize=2097152;//设置最大允许字节
$type=$myfile[&#39;type&#39;];//获取图片类型
$AllowTypr=array(&#39;image/jpeg&#39;,&#39;image/png&#39;,&#39;image/gif&#39;);//允许类型数组
if ($error==0){//为0,代表上传成功
  if ($size>$MaxSize){
    echo "<script>alert(&#39;文件大小超过限制&#39;);</script>";
    echo "<script>history.back(1);</script>";
    exit();
  }
  if (!in_array($type,$AllowTypr)){
    echo "<script>alert(&#39;请上传正确图片类型&#39;);</script>";
    echo "<script>history.back(1);</script>";
    exit();
  }

Related video recommendations: PHP programming from entry to proficiency

The above is the detailed content of What is the php file upload process?. For more information, please follow other related articles on the PHP Chinese website!

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