Home > Backend Development > PHP Problem > How to write php file upload code

How to write php file upload code

angryTom
Release: 2023-02-27 19:46:01
Original
3160 people have browsed it

How to write php file upload code

How to write the php file upload code

When we first learned php, we couldn’t understand the php file upload code , as well as the upload logic, the following code is written from a beginner's perspective, I hope it will be helpful to everyone!

Required knowledge:

Super global array: the value of $_FILES

$_FILES['myfile']['name'] Is: The file name of the client file system

$_FILES['myfile']['type'] The value is: The file type passed by the client

$_FILES['myfile'] The value of ['size'] is: the byte size of the file

The value of $_FILES['myflie']['tmp_name '] is: the temporary full path stored on the server after the file is uploaded

The value of

$_FILES['myfile']['error'] is: the error code of file upload

The value stored in $_FILES['myfile']['error']

A value of 0: indicates that no error occurred.

A value of 1: indicates that the size of the uploaded file exceeds the agreed value. The maximum file size is specified in the PHP configuration file. The instruction is: upload_max_filesize

The value is 2: Indicates that the uploaded file size exceeds the maximum value specified by the MAX_FILE_SIZE element of the HTML form hidden field attribute

A value of 3: Indicates that the file was only partially uploaded

A value of 4: Indicates that no files were uploaded

A value of 6: Indicates that the temporary folder cannot be found

The value is 7: Indicates that the file writing failed.

The error value corresponds to the constant

UPLOAD_ERR_OK: The corresponding value is 0

UPLOAD_ERR_INI_SIZE: The corresponding value is 1

UPLOAD_ERR_FORM_SIZE: Corresponding value 2

UPLOAD_ERR_PARTIAL: Corresponding value 3

UPLOAD_ERR_NO_FILE: Corresponding value 4

UPLOAD_ERR_NO_TMP_DIR: Corresponding value 6

UPLOAD_ERR_CANT_WRITE: Corresponding Value 7

upload.html The code is as follows

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>图片上传</title>
</head>
<body>
    <form action="upload.php" method="post" enctype="multipart/form-data">
        <input type="file"  name="pic"/>
        <!--通过表单限制上传文件的大小 一定要在上传按钮前 大小以字节为主-->
        <input type="hidden" name="MAX_FILE_SIZE"  value="8388608"/>
        <input type="submit" name="sub" value="上传" />
    </form>
</body>
</html>
Copy after login

upload.php The code is as follows

<?php
//var_dump($_FILES);
//exit;
//判断是否是文件上传
$file = $_FILES;
//第一个pic 是input 中的name值
if(is_uploaded_file($file[&#39;pic&#39;][&#39;tmp_name&#39;]))
{
    //声明允许上传的文件类型
    $allowType = [&#39;image/png&#39;,&#39;image/jpeg&#39;,&#39;image/gif&#39;,&#39;image/jpg&#39;];
    //正在上传的文件的类型
    $type = $file[&#39;pic&#39;][&#39;type&#39;];
//    echo $type;
    //判断类型是否是允许的类型
    if(!in_array($type,$allowType))//检查数组中是否存在某个值,返回布尔型
    {
       exit(&#39;上传类型有误&#39;);
    }
    //判断文件上传的大小
    if($file[&#39;pic&#39;][&#39;size&#39;] > 1024 * 1024 * 8) //以字节为主 1024*1024是1M
    {
        exit(&#39;上传的文件太大&#39;);
    }
    //设置新文件名
    $filename = date(&#39;YmdHis&#39;,strtotime(&#39;now&#39;)).rand(1000,9999);
    //获取上传文件的后缀名
    $name = $file[&#39;pic&#39;][&#39;name&#39;];
    //得到文件名字符串
    $filestr      = explode(&#39;.&#39;,$name);
    $ext = array_pop($filestr);
    //拼接新文件名
    $newfilename = $filename.&#39;.&#39;.$ext;
    //拼接上传文件的路径
    $path = &#39;./uploads/&#39;;
    //绝对路径
    $abspath = $path.$newfilename;
    if(move_uploaded_file($file[&#39;pic&#39;][&#39;tmp_name&#39;],$abspath))
    {
        echo &#39;上传成功&#39;;
    }else{
        echo &#39;上传失败&#39;;
    }
}
Copy after login

For more PHP related knowledge, please visit PHP Chinese website!

The above is the detailed content of How to write php file upload code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template