引言
最近失业在家,每天去图书馆阅读大量的书籍。
名句:
人这一生机会很多,重要的机会只有一个
创业成功绝对不是靠技术,而是靠市场
巨人公司的故事、企业助穷、apple vs 诺亚机、微软如何崛起
如何减压 and 对抗焦虑
平面构成(对称与平衡,对比与调和)
情商课
1. 关切对方(就像父母对话孩子一样的温暖,天气冷不冷啊)
2. 学会道歉,有没有吓到你了 (对不起给你添麻烦了)
3. 共鸣 (那我们把空调开起来吧)
如下代码
<?php
namespace app\http\controllers\api\air\v_1_0;
use app\http\controllers\core\apicontroller;
use app\models\loginlog;
use illuminate\http\request;
use illuminate\support\facades\db;
use oss\ossclient;
class filecontroller extends apicontroller
{
public function uploadfile(request $request)
{
$file = $request->file();
$ret = $this->_upload($file);
echo json_encode($ret);
exit;
}
protected function _upload($file)
{
$dir_name = empty($_get['dir']) ? 'image' : trim($_get['dir']);
$storage_path = config('upload.storage');
$webpath_path = config('upload.webpath');
$image_path = $dir_name . '/' . date("y-m") . '/';
$app_url = env('app_url') . '/';
if (!$file) {
return array('error'=>1,'message'=>"文件上传失败,请检查后重试");
}
$ossconfig = config('oss.' . config('upload.ossflag'));
if($ossconfig['bucket_addr']) {
$app_url = $ossconfig['bucket_addr'];
}
foreach ($file as $k=>$v) {
$tmpname = $v->getpathname();
$fileextension = $v->getclientoriginalextension();
$filepath = md5_file($tmpname) . '.' . $fileextension;
$file_size = $v->getsize();
if($file_size > config('upload.maxsize'))
{
return array('error'=>1,'message'=>"文件不可以超过50mb");
}
if (!in_array(strtolower($fileextension),config('upload.allowexts')))
{
return array('error'=>1,'message'=>"文件类型不支持");
}
//上传成功后处理
if(config('oss.' . 'oss_open') == 1) {
$oss = new ossclient($ossconfig['access_key_id'], $ossconfig['access_key_secret'], $ossconfig['endpoint']);
$res = $oss->uploadfile($ossconfig['bucket'],$webpath_path . $image_path . $filepath,$v->getpathname());
if ($res)
{
$data['oss'] = 1;
$data['attach_url'] = $ossconfig['bucket_addr'] . $webpath_path . $image_path . $filepath;
} else {
$data['attach_url'] = $app_url . $webpath_path . $image_path . $filepath;
$data['oss'] = 0;
}
$data['time'] = time();
$attach_id = db::table('attachment')->insertgetid($data);
$attach_array[] = $attach_id;
$attach_url[] = $data['attach_url'];
} else {
$data['oss'] = 0;
$data['attach_url'] = $app_url. $webpath_path . $image_path . $filepath;
$data['time'] = time();
$attach_id = db::table('attachment')->insertgetid($data);
$attach_array[] = $attach_id;
$attach_url[] = $data['attach_url'];
}
if(!$data['oss'] || config('oss.' . 'is_delete') != 1)
{
$v->move($storage_path . $image_path, $filepath);
unset($data);
}
return array('error'=>0,'attach'=>$attach_array,'url'=>$attach_url[0],'url_arr'=>$attach_url);
}
}
}以上就是laravel如何上传文件实现示例的详细内容,更多关于laravel上传文件的资料请关注代码网其它相关文章!
发表评论