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)

藏色散人
藏色散人Original
2018-09-17 17:35:086571browse

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>

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);

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;]);
}
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:
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