php 上传图片方法

这篇博客介绍了如何使用HTML的file输入框,JavaScript的Ajax和FormData对象,以及PHP的UploadFile类来实现用户头像上传功能。用户选择图片后,通过Ajax将文件发送到服务器,服务器端对文件大小和类型进行限制,然后保存并返回新的头像路径,前端接收到响应后更新头像显示。
摘要由CSDN通过智能技术生成
html代码
<input type="file" id="shangchuan" value="" />
<input type="hidden" name="head_pic" id="head_pic" value="<{$userInfo.head_pic}>"/>
<img src="/Public/Uploads/<{$userInfo.head_pic}>" alt="" id="head_pic_img">

js 代码

$("#shangchuan").change(function(){
   var formData = new FormData();
   formData.append("file",$("#shangchuan")[0].files[0]);
   $.ajax({
      url:"<{:U('Member/uploadImgs')}>",
      type:"post",
      data:formData,
      dataType:"json",
      mimeType:"multipart/form-data",
      processData: false,       //用于对data参数进行序列化处理 这里必须false
      contentType: false,    //必须
      success:function(result){
         if(result.status == 200){
            $("#head_pic").val(result.data.head_pic);
            $("#head_pic_img").attr("src","/Public/Uploads/"+result.data.head_pic);
            layer.msg("上传成功",{time:2000});
         }else{
            layer.msg(result.msg,{time:2000});
         }
      }
   })
})
php代码
protected function _upload_init($upload)
{
    $allow_max = 2048; //读取配置
    $allow_exts = explode(',', 'jpg,gif,png,jpeg,swf,bmp'); //读取配置
    $allow_max && $upload->maxSize = $allow_max * 1024; //文件大小限制
    $allow_exts && $upload->allowExts = $allow_exts; //文件类型限制
    $upload->saveRule = 'uniqid';
    return $upload;
}

/**
 * 上传头像接口
 */
public function uploadImgs()
{
    import('Org.Net.UploadFile');
    $upload = new \UploadFile(); // 实例化上传类

    $upload->maxSize = 9145728; // 设置附件上传大小
    //$upload->allowExts = explode(',', 'jpg,gif,png,jpeg,bmp'); // 设置附件上传类型
    $upload->saveRule = 'avatar'; //设置上传头像命名规则(临时图片),修改了UploadFile上传类
    //完整的头像路径
    $path =UPLOAD_PATH.'user/' . date('Ymd') . '/';

    if (!file_exists($path)) {

        mkdir($path);
    }
    $thumb_index = array(
        'width' => '75',
        'height' => '75',
        'remove_origin' => true,
        'isdate' => false,
    );
    if ($thumb_index) {
        $upload->thumb = true;
        $upload->thumbMaxWidth = $_REQUEST['width'] ?$_REQUEST['width']:$thumb_index['width'];
        $upload->thumbMaxHeight = $_REQUEST['height'] ?$_REQUEST['height']:$thumb_index['height'];
        $upload->thumbPrefix = '';
        $upload->thumbExt = isset($thumb_index['ext']) ? $thumb_index['ext'] : '';
        if($_REQUEST['file']){

            $upload->thumb_mode = $_REQUEST['file'];
        }
    }
    $upload->savePath = $path;
    $upload = $this->_upload_init($upload);

    if (!$upload->upload()) {
        // 上传错误提示错误信息
        $this->ajaxReturn(201, $upload->getErrorMsg());
    } else {
        // 上传成功 获取上传文件信息
        $info = $upload->getUploadFileInfo();
        $returnPath=array('head_pic'=>'/user/' . date('Ymd') . '/' . $info[0]['savename']) ;

        $this->ajaxReturn(200,'上传成功!', $returnPath);
    }
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浮生若梦01

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值