美文网首页
[PHP文件上传下载]⑤--多文件上传

[PHP文件上传下载]⑤--多文件上传

作者: 子木同 | 来源:发表于2017-09-19 14:12 被阅读37次
    Paste_Image.png

    test.php

    <html>
    <head><title></title></head>
    <body>
    <form action="doAction.php" method="post" enctype="multipart/form-data">
        请选择您要上传的文件:<input type="file" name="myFile[]"><br/>
        请选择您要上传的文件:<input type="file" name="myFile[]"><br/>
        请选择您要上传的文件:<input type="file" name="myFile[]"><br/>
        请选择您要上传的文件:<input type="file" name="myFile[]" multiple="multiple"><br/>
        <input type="submit" value="上传文件">
    </form>
    </body>
    </html>
    

    common.func.php

    <?php
    /**
     * 得到文件扩展名
     * @param $filename
     * @return string
     */
    function getExt($filename)
    {
        return strtolower(pathinfo($filename, PATHINFO_EXTENSION));
    }
    
    /**
     * 产生唯一字符串
     * @return string
     */
    function getUniName()
    {
        return md5(uniqid(microtime(true), true));
    }
    

    upload.func.php

    <?php
    /**
     * 构建上传文件信息
     * @return mixed
     */
    function getFiles()
    {
        $i = 0;
        foreach ($_FILES as $file) {
            if (is_string($file['name'])) {
                //单文件
                $files[$i] = $file;
                $i++;
            } else if (is_array($file['name'])) {
                foreach ($file['name'] as $key => $val) {
                    $files[$i]['name'] = $file['name'][$key];
                    $files[$i]['type'] = $file['type'][$key];
                    $files[$i]['tmp_name'] = $file['tmp_name'][$key];
                    $files[$i]['error'] = $file['error'][$key];
                    $files[$i]['size'] = $file['size'][$key];
                    $i++;
                }
            }
        }
        return $files;
    }
    
    function uploadFile($fileInfo, $path = './uploads', $flag = true,
                        $maxSize = 1048576, $allowExt = array('jpeg', 'jpg', 'gif', 'png'))
    {
        $res = null;
        //判断错误号
        if ($fileInfo['error'] === UPLOAD_ERR_OK) {
            if ($fileInfo['size'] > $maxSize) {
                $res['mes'] = $fileInfo['name'] . '上传文件过大';
            }
            $ext = getExt($fileInfo['name']);
            if (!in_array($ext, $allowExt)) {
                $res['mes'] = $fileInfo['name'] . '非法文件类型';
            }
            if ($flag && !$res['mes']) {
                if (!getimagesize($fileInfo['tmp_name'])) {
                    $res['mes'] = $fileInfo['name'] . "不是真实图片类型";
                }
            }
            if (!is_uploaded_file($fileInfo['tmp_name'])) {
                $res['mes'] = $fileInfo['name'] . "文件不是通过HTTP POST方式上传来的";
            }
            if ($res['mes']) {
                return $res['mes'];
            }
            if (!file_exists($path)) {
                mkdir($path, 0777, true);
                chmod($path, 0777);
            }
            $uniName = getUniName();
            $destination = $path . '/' . $uniName . '.' . $ext;
            if (move_uploaded_file($fileInfo['tmp_name'], $destination)) {
                $res['mes'] = $fileInfo['name'] . "文件移动失败";
            }
            $res['mes'] = $fileInfo['name'] . '上传成功';
            $res['dest'] = $destination;
            return $res;
    
    
        } else {
            switch ($fileInfo['error']) {
                case 1:
                    $res['mes'] = "上传文件超过了PHP配置文件中upload_max_file";
                    break;
                case 2:
                    $res['mes'] = "超过了表单MAX_FILE_SIZE";
                    break;
                case 3:
                    $res['mes'] = "文件部分被上传";
                    break;
                case 4:
                    $res['mes'] = "没有选择上传文件";
                    break;
                case 6:
                    $res['mes'] = "没有找到临时目录";
                    break;
                case 7:
                case 8:
                    $res['mes'] = "系统错误";
                    break;
            }
            return $res['mes'];
        }
    }
    

    doAction.php

    <?php
    //foreach ($_FILES as $fileInfo) {
    //    $files[] = uploadFile($fileInfo);
    //}其中有文件不符合类型会exit (file1 file2 file3 file4)
    
    //var_dump($_FILES);
    header('content-type:text/html;charset=utf-8');
    require_once 'upload.func.php';
    require 'common.func.php';
    
    $files = getFiles();
    //var_dump($files);
    foreach ($files as $fileInfo) {
        $res = uploadFile($fileInfo);
        echo $res['mes'], "<br/>";
        $uploadFiles[] = $res['dest'];
    }
    $uploadFiles = array_values(array_filter($uploadFiles));
    var_dump($uploadFiles);
    ?>
    
    Paste_Image.png Paste_Image.png

    相关文章

      网友评论

          本文标题:[PHP文件上传下载]⑤--多文件上传

          本文链接:https://www.haomeiwen.com/subject/lsvbsxtx.html