php 图片上传

/*

    * base64图片上传

    * @param string $base64_img 图片数据流

    * @return string|bool

    */

    public function base64img($base64_img)

    {

$up_dir = dirname(__DIR__).'/web/uplodes/image/';

if(preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_img, $result)){

$type = $result[2];

//判断图片类型

if(in_array($type,array('pjpeg','jpeg','jpg','gif','bmp','png')))

{

$new_file = $up_dir.date('YmdHis').'.'.$type;

$returnurl = Yii::$app->params['IMGURL'].'/uplodes/image/'.date('YmdHis').'.'.$type;

if(file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_img))))

{

return ['message'=>'上传成功','data'=>$returnurl,'code'=>200];

}else{

return ['message'=>'上传失败','data'=>[],'code'=>400];

}

}else{

//文件类型错误

return ['message'=>'类型错误','data'=>[],'code'=>400];

}

}else{

//文件错误

return ['message'=>'文件错误','data'=>[],'code'=>400];

}

    }

 

 

 

 

/**

* @api {post} uplode/uploadimg 图片路径

* @apiDescription 图片路径

* @apiParam {String} image 图片文件

* @apiGroup Uplodes

* @apiPermission ???

* @apiVersion 1.1.0

* @apiSuccessExample {json} Success-Response:

{

"code": "200",

"message": "上传成功",

"data":'图片地址'

        }

{

"code": "400",

"message": "上传失败"

}

*/

    // 文件上传

public function actionUploadimg()

{

        $file = $_FILES['image'];

        //接收参数

$request = Yii::$app->request->post();

//定义路径

        $imgurl = dirname(__DIR__).'/web/uplodes/image/'.date("Ym");

        

        if(!is_dir($imgurl)){

            mkdir($imgurl);

         }

//图片名称

$filename = '/'.md5(time().mt_rand(10, 99)).".png";

        $newFilePath = $imgurl.$filename;

        $returnurl = Yii::$app->params['IMGURL'].'/uplodes/image/'.date("Ym").$filename;

        //移动图片

        $isup = move_uploaded_file($_FILES['image']['tmp_name'], $newFilePath);

if($isup == true){

$info['imgurl'] = $imgurl;

return $this->response(['message'=>'上传成功','data'=>$returnurl,'code'=>200]);

}else{

return $this->response(['message'=>'上传失败','data'=>[],'code'=>400]);

}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值