PHP-实现多文件上传

以下是自己练习php文件上传时的代码,仅供参考学习

upload.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>多文件上传</title>
</head>
<body>
    <form action="doAction2.php" method="post" enctype="multipart/form-data">
        请选择您要上传的文件:
        <input type="file" name="myFile[]" multiple><br/>
        <input type="file" name="myFile[]" multiple><br/>
        <input type="file" name="myFile[]" multiple><br/>
        <input type="file" name="myFile[]" multiple><br/>
        <input type="submit" value="上传文件">
    </form>
</body>
</html>

doAction.php

<?php 
//print_r($_FILES);
header("content-type:text/html;charset=utf-8");
require_once 'upload.func1.php';
require_once 'common.func.php';
    $files=getFiles();
    foreach($files as $fileInfo){
        $res=uploadFile($fileInfo);
        echo $res['mes'],'<br/>';
        $res['dest'] = isset($res['dest'])?$res['dest']:null;
        $uploadFiles[]=$res['dest'];
    }
    $uploadFiles=array_values(array_filter($uploadFiles));
    print_r($uploadFiles);
?>

common.func.php

<?php
/**
 * 得到文件扩展名
 */
    function getExt($filename){
        return strtolower(pathinfo($filename,PATHINFO_EXTENSION));
    }
/**
 * 得到唯一的文件名
 */
    function getUniqname(){
        return md5(uniqid(microtime(true),true));
    }

upload.func.php

<?php
/**
 * 构建上传文件的信息
 */
function getFiles(){
    //多文件时,获得的数据是个三维数组,需要拆分才一一对应的数据
    $i=0;
    foreach($_FILES as $file){
        //判断是不是多文件,还是二维数组
        if(is_string($file['name'])){
            $files[$i] = $file;
            $i++;
        }elseif(is_array($file['name'])){
            foreach ($file['name'] as $key => $value) {
                $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;
}
/**
 * 针对于单文件、多个单文件、多文件的上传
 * @param array $fileInfo
 * @param string $path
 * @param string $flag
 * @param number $maxSize
 * @param array $allowExt
 * @return string
 */
function uploadFile($fileInfo,$path='./uploads',$flag=true,$maxSize=2097152,$allowExt=array('jpg','jpeg','gif','png')){
    /*$allowExt = array('jpg','jpeg','gif','png');
    $flag = true;
    $maxSize = 2097152; //自定义2M*/

    //判断错误号
    if($fileInfo['error']===0){
        //检测上传文件大小
        if($fileInfo['size']>$maxSize){
            $res['mes'] = $fileInfo['name'].'上传文件过大';
        }
        //检测上传文件的类型
        $ext = getExt($fileInfo['name']);
        if(!in_array($ext,$allowExt)){
            $res['mes'] = $fileInfo['name'].'非法文件类型';
        }
        //检测是否是真实的图片类型
        if($flag){
            if(!getimagesize($fileInfo['tmp_name'])){
                $res['mes'] = $fileInfo['name'].'不是真实的图片类型';
            }
        }
        //检测是否从HTTP POST表单上传
        if(!is_uploaded_file($fileInfo['tmp_name'])){
            $res['mes'] =  $fileInfo['name'].'文件不是通过POST方式上传的';
        }
        if(!empty($res)){
            return $res;
        }
        //开始移动临时文件到指定的文件位置
        /*$path = './uploads';*/
            if(!file_exists($path)){
                mkdir($path,0777,true);
                chmod($path,0777);
            }
        $uniqName = getUniqname();
        $destination = $path.'/'.$uniqName.'.'.$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'] = '上传的文件超过了服务端配置中upload_max_filesize选项限制的值';
                        break;
                    case 2:
                        $res['mes'] = '上传的文件超过了HTML表单POST中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;
    }
}
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值