zoukankan      html  css  js  c++  java
  • PHP多文件上传代码练习

    HTML表单:

    <html>
        <head><title>upload file</title>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
        </head>
        <body>
            <form action="test.php" method="post" enctype="multipart/form-data">
                <!-- name必须是MAX_FILE_SIZE,value是字节数 -->
                <input type="hidden" name="MAX_FILE_SIZE" value="2097152"/>
                <!-- accept是文件的MIME类型 -->
                <!-- nultiple="nultiple" 是代表可以多选 -->
                <input type="file" name="fileName[]"  multiple="multiple" accept="image/jpeg,image/gif,image/png,application/x-MS-bmp,text/plain,text/html" /><br><br>
                <input type="submit" value="上传文件"/>
            </form>
        </body>
    </html>

    PHP多文件上传函数表单uploads.func.php

    <?php
    
    function getFiles(){
    
        //把三维数组改成两维数组;
        $i=0;
        foreach ($_FILES as $file) {
    
            if (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]['size']=$file['size'][$key];
                    $files[$i]['error']=$file['error'][$key];
                    $i++;
                    # code...
                }
            }elseif (is_string($file['name'])) {
    
                $files[$i]=$file;
                $i++;
                # code...
            }
             # code...
         }
    
         return $files;
    }
    
    function uploadFiles($files,$path='uploads',$maxsize=2097152){ 
        $result=array();
                         
          //判断$files['error']
        if ($files['error']===UPLOAD_ERR_OK) {
    
            //判断文件大小
            //$maxsize=2097152;
            if ($files['size']>$maxsize) {
    
                $result['msg']=$files['name'].'文件过大';
                # code...
            }
    
            //判断文件格式
            $ext=@strtolower(end(explode('.', $files['name'])));
            $extarray=array('gif','jpeg','jpg');
            if (!in_array($ext, $extarray)) {
    
                $result['msg']=$files['name'].'文件格式不正确';
                # code...
            }
    
            //检查是否是真实的图片
            $check= true;
            if ($check) {
                
                if(@!getimagesize($files['tmp_name'])){
    
                    $result['msg']=$files['name'].'不是真正的图片';
                }
                # code...
            }
    
            //判断是否是通过HTTP Post上传
            if (!is_uploaded_file($files['tmp_name'])) {
    
                $result['msg']=$files['name'].'不是通过HTTP POST上传';
                # code...
            }
    
            if($result) return $result;//返回$result;
    
            //移动文件
             //指定上传目录判断并创建目录
            //$path='uploads';
            if (!file_exists($path)) {
    
                mkdir($path,0777,true);
                chmod($path, 0777);
                # code...
            }
    
            //唯一的文件名
            $newname=md5(uniqid(microtime(true),true)).'.'.$ext;
            $destination=$path.'/'.$newname;
    
            if(!move_uploaded_file($files['tmp_name'], $destination)){
    
                $result['msg']=$files['name'].'文件移动失败';
    
            }
    
            $result['msg']=$files['name'].'上传成功';
    
            $result['dest']=$destination;
            
            return $result;
    
        }else{
            switch($files['error']){
                case 1:
                    $result['msg']=$files['name']. "文件超过了php.ini中upload_max_filesize选项限制的值。";
                     break;
                   
                   case 2:
                       $result['msg']=  $files['name']."文件的大小超过了HTML表单中max_file_size选项指定的值。";
                       # code...
                       break;
                   case 3:
                       $result['msg']=  $files['name']."文件只有部分被上传";
                       # code...
                       break;
                   case 4:
                      $result['msg']=  "没有文件被上传";
                       # code...
                       break;
                   case 6:
                       $result['msg']=  "找不到临时文件夹。";
                       # code...
                       break;
                   case 7:
                   case 8:
                       $result['msg']=  "系统出错";
                       # code...
                       break;
            }
    
            return $result;
        }
    
        
    }
    
    ?>
                 

    upload.php

    <?php
    include('uploads.func.php');
    
    $files=getFiles();
    
    foreach ($files as $value) {
    
        $result=uploadFiles($value);
        echo $result['msg']."<br/>";
        $uploads[]=@$result['dest'];
    }
    
     $uploads=array_values(array_filter($uploads));
      print_r($uploads);
    //当array_filter函数的callback留空时 他会过滤掉所有键值为false的键 
    //array_values() 函数返回一个包含给定数组中所有键值的数组,但不保留键名。
  • 相关阅读:
    redis学习
    Ubuntu命令大全
    关于jquery中attr和prop的用法
    Ubuntu下修改为永久DNS的方法
    Yii2 behaviors中verbs access的一些理解
    vue_ form表单 v-model
    vue-one_demo_music
    ES6
    VUE 入门 01
    Django model.py表单设置默认值允许为空
  • 原文地址:https://www.cnblogs.com/perseverancevictory/p/4281720.html
Copyright © 2011-2022 走看看