上传图片

admin1年前Laravel516

public function image()
{
   $obFile = request()->file('image');
   if (!$obFile) {
       return $this->error('请上传文件');
   }
   $sOldName = $obFile->getClientOriginalName();//原名
   $sFileType = $obFile->getClientMimeType(); //文件类型
   $sExtensionName = $obFile->getClientOriginalExtension();//扩展名
   $sPath = $obFile->getRealPath();    //文件存储的位置路径
   $arrAllowExtension = ['jpg', 'png', 'jpeg', 'gif', 'bmp'];
   if (!in_array($sExtensionName, $arrAllowExtension)) {
       return $this->error('必须是 jpeg, bmp, png, gif chan');
   }
   //组装文件存储的位置和自定义文件名
   $path = "upload/goods/" . date("Ymd");
   if (!is_dir($path)) { //判断目录是否存在 不存在就创建
       mkdir($path, 0777, true);
       chmod($path, 0777);
   }
   $imageName = "p_" . date("His", time()) . "_" . rand(1111, 9999);
   $sFileName = 'goods/' . date('Ymd') . '/' . $imageName . '.' . $sExtensionName;
   //调用laravel系统封装好的函数把图片存储到指定文件下
   $bRet = Storage::disk('admin')->put($sFileName, file_get_contents($sPath));
   if (!$bRet) {
       return $this->error('上传失败');
   }
   return $this->success('ok', $this->domain() . '/upload/' . $sFileName);
}

相关文章

关注公众号并登录

/* * 服务端生成带唯一标识二维码并将唯一标识返回给前端 */ public function wx_code() { $scene_str = $this->get_...

验证手机号

public function phone($phone){    $pat = '/'        . '^13...

laravel 加cache锁

use Illuminate\Support\Facades\Redis; use Illuminate\Support\Facades\Cache; $lock = Ca...

手机号替换

 $vv = substr_replace($vv, '****', 3, 4) ....

解密微信绑定的手机号

public function mobile() { $code = request()->input('code'); if (!$code) { return $this-&...

修改密码

public function setpass(){    $mobile = request()->input('mobile');   &nbs...