layUI图片上传 php代码

PHP代码

"; 
    //echo $_FILES['file']['name'];
    exit("ERROR");
 }   
//生成目标文件的文件名       
else{   
$filename=explode(".",$_FILES['file']['name']);   
 do   
 {   
      $filename[0]=random(10); //设置随机数长度   
     $name=implode(".",$filename);   
        //$name1=$name.".Mcncc";   
     $uploadfile=$uploaddir.$name;   
  }   
while(file_exists($uploadfile));   
 if (move_uploaded_file($_FILES['file']['tmp_name'],$uploadfile)){ 
     //$uploadfile=str_replace("../","/",$uploadfile);
      $arr=array("code"=>1,"msg"=>"上传成功","src"=>$uploadfile);
       $myfile = fopen("file.txt", "w") or die("Unable to open file!");
     fwrite($myfile, $uploadfile);
      fclose($myfile);
   }
  else
   {
      
       $arr=array("code"=>0,"msg"=>"上传失败");

  } 
echo json_encode($arr);  
}    

?>

前台JS

layui.use('upload', function(){
  var upload = layui.upload;
  //上传文件
  var uploadInst = upload.render({
    elem: '#upfile' //绑定元素
    ,url: 'upload.php' //上传接口
    ,done: function(res){
if(res['code']==1){
//alert(res['src']);
var imgpath=res['src'];
//alert(imgpath);
layer.msg('图片上传成功');
$("#proimg").attr('src',imgpath);
$("#inputimg").val(imgpath);
}else{
layer.msg('图片上传失败');
}
 
    }
  });
});

你可能感兴趣的:(layUI图片上传 php代码)