php图片上传模板,图片上传封装 - 大海技术博客,大海博客,技术博客,个人博客模板, php博客系统...

图片上传封装

/**

* 上传图片

*

*/

class ImageFile

{

public static function uploadImageFile($savePath, $key = 'files', $allowExt = [], $maxSizeM = 10)

{

$return = ['ret' => 0, 'msg' => 'success'];

try {

if (!$savePath) {

$return['ret'] = 1;

$return['msg'] = 'The file save path is empty';

return $return;

}

if (empty($_FILES[$key]['name'])) {

$return['ret'] = 1;

$return['msg'] = 'file not exist';

return $return;

}

$fileErrors = $_FILES[$key]['error'];

foreach ((array)$fileErrors as $fileError) {

if ($fileError != 0) {

$return['ret'] = 1;

$return['msg'] = 'file upload error';

return $return;

}

}

$fileSize = $_FILES[$key]['size'];

if ($fileSize / 1024 / 1024 > $maxSizeM) {

$return['ret'] = 1;

$return['msg'] = 'file size too large';

}

$fileName = $_FILES[$key]['name'];

$filePath = $_FILES[$key]['tmp_name'];

$fileExt = pathinfo($fileName)['extension'];

if (!in_array($fileExt, $allowExt)) {

throw new Exception('File format only supports: ' . implode('、', $allowExt));

}

$fileName = pathinfo($fileName)['filename'];

$object = __PUBLIC__ . $savePath . '/' . $fileName . '.' . $fileExt;

//自己处理业务逻辑

;

$return['data'] = ["imageUrl"=>$data];

return $return;

} catch (\Exception $e) {

$return['ret'] = 1;

$return['msg'] = $e->getMessage();

return $return;

}

}

}

调用

/**

* 图片上传

*/

public function uploadImage()

{

$savePath = "/dev/data/dahai/hpay_image";

$res = ImageFile::uploadImageFile($savePath,'file',['png'], 2);

return $this->returnJson($res);

}

直接上传图片资源就行了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值