Home Daily Programming PHP Knowledge How to use multiple attribute in PHP to upload multiple files and parse information? (Pictures + Videos)

How to use multiple attribute in PHP to upload multiple files and parse information? (Pictures + Videos)

Sep 17, 2018 pm 05:35 PM

This article will continue to introduce how to use the multiple attribute in HTML5 to implement PHP multi-file upload and information parsing.

So what are the simple methods for uploading multiple files in PHP? ], I have briefly introduced some simple methods for uploading multiple files in PHP. Friends who need it can refer to it.

Below we will use specific code examples to introduce in detail the complete method of using PHP's multiple attribute to implement multiple file uploads and information parsing.

First the HTML form code is as follows:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="getFile.php" method="post" enctype="multipart/form-data">
 选择文件进行上传: <input type="file" name="file[]" multiple=""><br>
    <input type="submit" value="上传">
</form>
</body>
</html>
Copy after login

We choose to upload multiple pictures and then access them through the browser. The effect is as follows:

How to use multiple attribute in PHP to upload multiple files and parse information? (Pictures + Videos)

How to use multiple attribute in PHP to upload multiple files and parse information? (Pictures + Videos)

How to use multiple attribute in PHP to upload multiple files and parse information? (Pictures + Videos)## Then upload the file to the getFile.php file. The PHP code example is as follows:

var_dump($_FILES);
Copy after login

This The result when accessed through the browser is as follows:


How to use multiple attribute in PHP to upload multiple files and parse information? (Pictures + Videos)At this time, you can see that we have obtained the three-dimensional array in the picture above.

So if we want to analyze the information of multiple uploaded files, we need to assemble the uploaded information first.

Then the complete getFile.php code is as follows:

<?php
/**
 * 组装多文件上传信息
 */
$files = [];
function getFile()
{
    $i = 0;
    foreach ($_FILES as $file) {
        if (is_string($file[&#39;name&#39;])) {
            $files[&#39;$i&#39;] = $file;
            $i++;
        } elseif (is_array($file[&#39;name&#39;])) {
            foreach ($file[&#39;name&#39;] as $k => $v) {
                $files[$i][&#39;name&#39;] = $file[&#39;name&#39;][$k];
                $files[$i][&#39;type&#39;] = $file[&#39;type&#39;][$k];
                $files[$i][&#39;tmp_name&#39;] = $file[&#39;tmp_name&#39;][$k];
                $files[$i][&#39;error&#39;] = $file[&#39;error&#39;][$k];
                $files[$i][&#39;size&#39;] = $file[&#39;size&#39;][$k];
                $i++;
            }
        }
    }
    return $files;
}

/**
 * 文件上传
 * @param $fileInfo
 * @param string $upload
 * @param array $imagesExt
 * @return string
 */
function upload_file($fileInfo, $upload = "./upload", $imagesExt = [&#39;gif&#39;, &#39;png&#39;, &#39;jpg&#39;])
{
    $res = [];
    if ($fileInfo[&#39;error&#39;] === 0) {
        $ext = strtolower(pathinfo($fileInfo[&#39;name&#39;], PATHINFO_EXTENSION));
        if (!in_array($ext, $imagesExt)) {
            $res[&#39;mes&#39;] = "文件非法类型";
        }
        if (!is_dir($upload)) {
            mkdir($upload, 0777, true);
        }
        if ($res) {
            return $res;
        }
        $fileName = md5(uniqid(microtime(true), true)) . "." . $ext;
        $destName = $upload . "/" . $fileName;
        if (!move_uploaded_file($fileInfo[&#39;tmp_name&#39;], $destName)) {
            $res[&#39;mes&#39;] = "文件上传失败!";
        }
        $res[&#39;mes&#39;] = $fileInfo[&#39;name&#39;] . "文件上传成功!";
        $res[&#39;dest&#39;] = $destName;
        return $res;
    } else {
        switch ($fileInfo[&#39;error&#39;]) {
            case 1:
                $res[&#39;mes&#39;] = &#39;上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值&#39;;
                break;
            case 2:
                $res[&#39;mes&#39;] = &#39;上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值&#39;;
                break;
            case 3:
                $res[&#39;mes&#39;] = &#39;文件只有部分被上传&#39;;
                break;
            case 4:
                $res[&#39;mes&#39;] = &#39;没有文件被上传&#39;;
                break;
            case 6:
                $res[&#39;mes&#39;] = &#39;找不到临时文件夹&#39;;
                break;
            case 7:
                $res[&#39;mes&#39;] = &#39;文件写入失败&#39;;
                break;
        }
        return $res;
    }
}

$files = getFile();

foreach ($files as $fileInfo) {
    $res = upload_file($fileInfo);
    echo $res[&#39;mes&#39;];
    var_dump($res[&#39;dest&#39;]);
}
Copy after login
In the above code, we first pass the

foreach loop judgment statement

to check the uploaded Judge the multi-dimensional array informationReorganize, and then create the upload_file method to parse the upload information of multiple files. The upload_file method here has been introduced in detail in this article [

Detailed explanation of PHP file upload method and its information analysis

], you can choose to refer to it. The above is the specific method of implementing multi-file upload and information analysis in PHP. If you want to know more about PHP, you can follow the

PHP video tutorial

on the PHP Chinese website!

The above is the detailed content of How to use multiple attribute in PHP to upload multiple files and parse information? (Pictures + Videos). For more information, please follow other related articles on the PHP Chinese website!

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)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months 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)