点击图像上传图片

html页面:

<link rel="stylesheet" href="https://cdn.bootcss.com/weui/1.1.2/style/weui.min.css">

<form id="imageform" method="post" enctype="multipart/form-data" action='{:U("User/accounthead_save")}'>  
<a>
<p ><img style="width:90px;height:90px;border-radius:50px;cursor: pointer" src="
{:getimg($userinfo['headimg'],0,'w_90,h_90')}">
<input id="headimg" name="headimg" class="weui-uploader__input" type="file" accept="image/*" onchange="showPicture(this)">
</p>
</a>

</form>

<script src="https://cdn.bootcss.com/jquery.form/4.2.2/jquery.form.min.js"></script>
<script type="text/javascript">
function showPicture(imgFile){
var rightFileType = new Array('jpg', 'bmp', 'gif', 'png', 'jpeg');  
var filextension = $(imgFile).val().substring($(imgFile).val().lastIndexOf('.') + 1);
if ($.inArray(filextension.toLowerCase(), rightFileType)==-1) {
alert("只支持图片文件上传!"); 
return;
};
$("#imageform").ajaxForm(function(data){
console.log(data);
if(data.status==1){
//alert('上传成功!');
location.reload();//刷新当前页面
}else{
alert('上传失败,请重传!');
$("#headimg").val("");
}
d.close().remove();
}).submit(); 
}

</script>

php页面:

//头像保存
function accounthead_save(){
$this->_upload();
if(I("post.headimg")){
$rs=M("users")->where(array('user_id'=>$this->userinfo['user_id']))->save(array("headimg"=>I("post.headimg")));
if($rs){
                 $this->ajaxReturn(array("status"=>'1',"strmsg"=>"上传成功"));
}else{
$this->ajaxReturn(array("status"=>'-1',"strmsg"=>"上传失败"));
}
}

}

//上传图片
public function _upload($type){ 
if(!empty($_FILES)){
foreach($_FILES as $v){
$upload = new \Think\Upload();// 实例化上传类  
$upload->maxSize = 4000000 ;// 设置附件上传大小
$upload->exts = $type == 1? array('pdf','doc','DOC','docx','xlsx','xls') : array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类
$filetype = $type == 1 ? "files" : "images";
$upload->rootPath   = getcwd() . '/Uploads/'.$filetype.'/';
//$upload->saveName = md5(uniqid());
$upload->saveName = array('uniqid', mt_rand(1,999999).'_'.md5(uniqid()));
$info = $upload->upload();
//附件类型
//$ext = $info['file']['ext'] == 'doc'? 'word' : 'pdf';
if(!$info) {     
//上传错误提示
//$this->error($upload->getError());    
//return false;
}else{
$config = C('ALIYUN_CONFIG');
vendor('aliyun.autoload');
$ossClient = new \OSS\OssClient($config['accessKeyId'], $config['accessKeySecret'], $config['endpoint']);

foreach ($info as $key => $value) {
foreach ($_FILES as $key1 => $value1) {
if($value['name']===$value1['name']){
//$_POST[$key1] = $value['savepath'].$value['savename'];
$object = 'Uploads/'.$filetype.'/'.$value['savepath'].$value['savename'];
$file = './'.$object;
try{
$ossClient->uploadFile($config['bucket'], $object, $file);
$_POST[$key1] = $config['http'].'/'.$object;
if($config['isUnlink']){//是否删除本地文件
unlink($file);
}
} catch(OssException $e) {
return;
}

if($type == 1){
$return['url'] = $_POST[$key1];
$return['ext'] = $ext;
return $return;
}
}
}
}
return true;
}
}
}else{
$_POST['user_headimg'] = "1";
}

}

猜你喜欢

转载自blog.csdn.net/weixin_38615720/article/details/79931140