上传图片

admin8个月前Laravel326

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 img_url() { $id = request()->param('id', 1122); $info = Db::name('...

laravel 加cache锁

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

获取域名

/** * 获取域名 */public function domain(){    return request()->getSchemeAndHttpHost();}...

按照距离远近排序

   $distance = "ACOS(SIN(( $lat * 3.1415) / 180 ) *SIN((lat * 3.1415) / 180 ) +COS((...

设置项目编号

$max = Db::table('member')->max('id') ?? 0; if ($max < 100000) { $cd = $this->create...

忘记密码

publicfunction forget(){    $phone = request()->input('phone');    $c...