PHP 记录自己的图片上传(单图和多图)

单图上传:
html:

 // 上传按钮
  //返回路径
![](__ASSETS__/images/uploadfile.png) // 图片显示

js:


  

处理图片上传

public function files(){
        if($_FILES['file']['error'] == 0){
            $type_array = array('image/jpeg','image/pjpeg','image/gif','image/png','application/octet-stream'); 
            if($_FILES['file']['size'] <= 3000000){   //判断图片大小
                $name = $_FILES['file']['name'];
                $name_arr = explode('.', $name);
                $new_name = time().uniqid().'.'.$name_arr[count($name_arr)-1];
                $path = 'Public/Console/images/admin/'.date('Y',time()).'/'.date('m',time());   //上传图片路径
                $new_path = $path.'/'.$new_name;
                
                if(!is_dir($path)){
                    mkdir($path,0777,true);
                }
                if(move_uploaded_file($_FILES['file']['tmp_name'],$new_path)){
                    echo $new_path;die;    //返回路径
                }else{
                    echo "";
                }
            }else{
                echo "";
            }
        }
    }

多图上传 和单图没啥区别,就是循环上传
html:

    //multiple="multiple"要有这个

js:


处理多图图片上传

public function filesall(){
        $i = 0;
        $length = count($_FILES['file']['name']);
        $img = array();
        for($i;$i<$length;$i++){    //接收过来的东西, 处理一下
            $img[$i]['name'] = $_FILES['file']['name'][$i];
            $img[$i]['type'] = $_FILES['file']['type'][$i];
            $img[$i]['tmp_name'] = $_FILES['file']['tmp_name'][$i];
            $img[$i]['error'] = $_FILES['file']['error'][$i];
            $img[$i]['size'] = $_FILES['file']['size'][$i];
        }    
        foreach ($img as $v){    //循环上传
            if($v['error'] == 0){
                $type_array = array('image/jpeg','image/pjpeg','image/gif','image/png','application/octet-stream');
                if($v['size'] <= 5000000){
                    $name = $v['name'];
                    $name_arr = explode('.', $name);
                    $new_name = time().uniqid().'.'.$name_arr[count($name_arr)-1];
                    $path = 'Public/Console/images/admin/'.date('Y',time()).'/'.date('m',time());
                    $new_path = $path.'/'.$new_name;
                    
                    if(!is_dir($path)){
                        mkdir($path,0777,true);
                    }
                    if(move_uploaded_file($v['tmp_name'],$new_path)){
                        $ppp .= '/'.$new_path.',';   //循环里面用逗号拼接一下字符串
                    }else{
                        echo "";
                    }
                }else{
                    echo "";
                }
            }
        }
        if($ppp){    //返回上传后的路劲
            echo $ppp;die;
        }
    }

你可能感兴趣的:(PHP 记录自己的图片上传(单图和多图))