PHPは自分の画像を記録してアップロードする(単図と多図)

4432 ワード

単図アップロード:html:
 //     
  //    
![](__ASSETS__/images/uploadfile.png) //     

js:

  
        $(function(){
            $("body").on("change","#up_img",function(){
                $.ajaxFileUpload({  
                    url : "{:U('Console/Console/files')}",   //submit to UploadFileServlet  
                    secureuri : false,  
                    dataType : 'text',    //       
                    fileElementId : 'up_img',     //file     ID
                    success : function(data) { 
                        $("#path_a").val('/'+data);     //            input     data      
                        $("#imgShow").attr("src",'/'+data);   //    
                    },  
                });
            })
        })
  

画像のアップロード処理
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 "alert('      !')";
                }
            }else{
                echo "alert('      !')";
            }
        }
    }

多図アップロードと単図は何の違いもありません.htmlをループアップロードします.
    //multiple="multiple"    


js:

        $(function(){
            var j = 0;     //              ,            
            $("body").on("change",".aaa",function(){
                var f = $("#up-img").val();
                $.ajaxFileUpload({  
                    url : "{:U('Console/Console/filesall')}",   //submit to UploadFileServlet  
                    secureuri : false,  
                    dataType : 'text',
                    fileElementId : 'up-img',   
                    success : function(data) {
                        var path = $("#path").val();
                        var newpath = path+data;
                        $("#path").val(newpath);
                        var str=data.substring(0,data.length-1)    //               
                        var arr=str.split(',');
                        for(var i=0;i<arr.length;i++){   //      
                            $("#imgshow").append("<div style='float:left;width:150px;height:150px;margin-right:10px;margin-bottom:10px;position:relative;' id='divimg"+j+"'>![]("+arr[i]+")<a style='position:absolute;right:0;top:0;background:#ccc;color:#fff;width:22px;height:22px;text-align: center;line-height:22px;cursor:pointer;' onclick='clearimg("+j+")'>×</a></div>");
                            j++
                        }
                    },  
                })
            })
        })
  

マルチピクチャアップロードの処理
public function filesall(){
        $i = 0;
        $length = count($_FILES['file']['name']);
        $img = array();
        for($i;$ialert('      !')";
                    }
                }else{
                    echo "alert('      !')";
                }
            }
        }
        if($ppp){    //        
            echo $ppp;die;
        }
    }