zoukankan      html  css  js  c++  java
  • 批量上传

    表格:
    <?php
     header("content-type:text/html;charset=utf-8");
    ?>
    <form action="do_form.php" method="post" enctype="multipart/form-data">
        <center>
            <table>
                <tr>
                    <td>图片名称:</td>
                    <td><input type="text" name="name"></td>
                </tr>
                <tr>
                    <td>请选择要上传的文件:</td>
                    <td>
                        <input type="file" name="myfile[]">
                        <input type="file" name="myfile[]">
                        <input type="file" name="myfile[]">
                    </td>
                </tr>
                <tr>
                    <td><input type="submit" value="提交"></td>
                    <td><input type="reset" value="重置"></td>
                </tr>
            </table>
        </center>
    </form>


    上传类:
    <?php
    header("content-type:text/html;charset=utf-8");
    class uploads{
        public $error_message;
        public $type = array('image/jpeg', 'image/jpg', 'image/gif', 'image/png');
        public $size = 4;
        public $path = "./image/";

        function upload($image)
        {
            if (empty($image['name'])) {
                $this->error_message="文件不能为空!";
                return false;
            }
            if (!$this->check_type($image['type'])) {
                return false;
            }
            if (!$this->check_size($image['size'])) {
                return false;
            }
            if($newpath=$this->check_dir()){
               $db=$newpath.$image['name'] ;
                if(move_uploaded_file($image['tmp_name'],$db)){
                    return $db;
                }else{
                    return false;
                }
            }else{
                return false;
            }
        }
     function check_type($type){
         if(in_array($type,$this->type)){
             return true;
         }else{
             $this->error_message="文件类型错误!";
             return false;
         }
     }
        function check_size($size){
            if($size>$this->size*1024*1024){
                $this->error_message="文件过大!";
                return false;
            }else{
                return true;
            }
        }
        function check_dir(){
            $dir=date('Y-m-d')."/";
            $newpath=$this->path.$dir;
            if(!file_exists($newpath)){
                mkdir($newpath,0777,true);
            }
            return $newpath;
        }
        function error(){
            return $this->error_message;
        }
    }

    批量上传:
    <?php
    header("content-type:text/html;charset=utf-8");
    $name=$_POST['name'];
    $myfile=$_FILES['myfile'];
    //print_r($myfile);
    //die;
    require "./upload.class.php";
    $obj=new uploads();
    require "./preg.class.php";
    $obj1=new check;
    require "./db.class.php";
    $obj3=new DB('root','root','user');
    foreach($myfile as $key=>$val){
        foreach($val as $k=>$v){
            $arr[$k][$key]=$v;
        }
    }
     foreach($arr as $a=>$b){
        $info=$obj->upload($b);
         if($info){
             $info1=$obj1->check_name($name);
             if($info1){
                 $sql="insert into img values(null,'$name','$info')";
                 $res=$obj3->query($sql);
                 if($res){
                     echo "入库成功!";
                 }else{
                     echo "入库失败!";
                 }
             }else{
                 echo $obj1->error();
             }
         }else{
             echo $obj->error();
         }
    }

  • 相关阅读:
    Ubuntu-18.04设置花生壳内网穿透
    Ubuntu开启SSH
    Ubuntu查看版本信息
    Linux之使用mount挂载ISO镜像
    druid参数配置说明
    [转]Java多线程学习(吐血超详细总结)
    eclipse 更改官方配色
    Spring AOP拦截对Controller的请求时的配置失败
    Spring Security3详细配置
    “java.lang.IllegalArgumentException: Failed to evaluate expression ‘ROLE_USER’”报错的解决
  • 原文地址:https://www.cnblogs.com/ymk0375/p/6257735.html
Copyright © 2011-2022 走看看