html
<div class="img_box"> <img src="" id="scimg" onclick="upimg()"> <input type="file" class="noshow" id="uploadimg"> <input type="hidden" name="headimg" id="headimg" value="1"> </div>
js
/*上传图片*/ function upimg(){ $("#uploadimg").click(); } /*上传图片具体方法*/ /*图片*/ $("#uploadimg").change(function(e) { var imgBox = e.target; var file = imgBox.files[0]; var form=new FormData(); form.append('file',file); $.ajax({ url:"<{:url('BaseController/upload')}>", data:form, type:"post", contentType: false,// jQuery不要去设置Content-Type请求头 processData: false,// jQuery不要去处理发送的数据 success:function(data){ console.log(data); $("#scimg").attr('src',data); $("#headimg").val(data); } }) });
php
/*上传图片*/ public function upload(){ // 获取表单上传文件 例如上传了001.jpg $file = request()->file('file'); // 移动到框架应用根目录/uploads/ 目录下 $info = $file->move( './upload'); if($info){ $getSaveName=str_replace("\","/",$info->getSaveName()); $path='/upload/'.$getSaveName; echo $path; } }