> 文件,圖片上傳保存到數據庫
`
~~~
/**
* @param $type
* @param string $filename
* @param bool $is_water
* @param int $width
* @param int $height
* @return array|bool
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
* @author: LuckyHhy <jackhhy520@qq.com>
* @date: 2020/1/13
* @name: upfile
* @describe: 上傳文件
*/
public function upfile($type, $filename = 'file', $is_water = false, $width = 200, $height = 400)
{
// 獲取表單上傳文件 例如上傳了001.jpg
$file=Request::file($filename);
//先檢測MD5和哈希是否存儲過
$md5 = $file->hash('md5');
$sha1 = $file->hash('sha1');
$is_up = $this->checkFileIsUp($md5, $sha1, $this->setting['upload_type']);
if($is_up!==true) {
//上傳過了
$is_up['code'] = 1;
$is_up['msg'] = '上傳成功';
$is_up['info']['name'] = $file->getInfo('name');
return $is_up;
}
//type 2 七牛 3 oss 1 本地
if($this->setting['upload_type']==2) {
//七牛云存儲
$tmp_name=$file->getInfo('tmp_name'); //臨時目錄
//文件后綴
$ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
$config = $this->setting['qiniu_config'];
// 構建一個鑒權對象
$auth =new Auth($config['accessKey'],$config['secretKey']);
// 生成上傳的token
$token = $auth->uploadToken($config['bucket']);
// 上傳到七牛后保存的文件名
$up_file_name=date('Ymd') . '/' . md5($tmp_name . time().mt_rand(0,9999));
$key = $up_file_name . '.' .$ext;
// 初始化UploadManager類
$uploadMgr = new UploadManager();
list($ret,$err) = $uploadMgr->putFile($token,$key,$tmp_name);
if($err !== null){
return array('code'=>0,'msg'=>$err);
}else{
$file_path=$config['domain'] . '/' . $key;
//保存上傳歷史
$arr=['file_size'=>$file->getInfo('size'),'create_time'=>time(),'filename'=>$file->getInfo('name'),'file_path'=>$file_path,'file_md5'=>$md5,'file_sha1'=>$sha1,'suffix'=>$file->getInfo('type'),'up_type'=>2];
$this->saveUpload($arr);
return array('code'=>200,'msg'=>'上傳成功','path'=>$file_path,'savename'=>$up_file_name,'filename'=>$file->getInfo('name'),'info'=>$file->getInfo());
}
}
else if($this->setting['upload_type']==3) {
return ['code' => 0, 'msg' => '上傳方式不存在'];
}
else {
// 移動到框架應用根目錄/uploads/ 目錄下
$info = $file->validate(['size' => $this->setting['size'], 'ext' => $this->setting['ext']])->move(ROOT_PATH.'public'.DS_PROS.'uploads'.DS_PROS.$type);
//$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . $type);
if($info) {
$path = '/uploads'.DS_PROS.$type.DS_PROS.$info->getSaveName();
//如果需要添加水印
/*$setting = cache('settings');
if($is_water && $setting['is_watermark'] && $setting['watermark'] && $type = 'images' ){
$image = \think\Image::open(ROOT_PATH . $path);
if($image->width() >= $setting['watermark_width'] && $image->height() >= $setting['watermark_height']){
$image->water(ROOT_PATH . $setting['watermark'],$setting['watermark_locate'],$setting['watermark_alpha'])->save(ROOT_PATH . $path);
}
}*/
$path = str_replace("\\", "/", $path);
//保存上傳歷史
$arr = ['file_size' => $info->getSize(), 'up_type' => $this->setting['upload_type'], 'ext' => $info->getExtension(), 'create_time' => time(), 'filename' => $file->getInfo('name'), 'file_path' => $path, 'file_md5' => $md5, 'file_sha1' => $sha1, 'suffix' => $info->getType()];
$getid = $this->saveUpload($arr);
return ['code' => 1, 'msg' => '上傳成功', 'save_id' => $getid, 'path' => $path, 'savename' => $info->getSaveName(), 'filename' => $info->getFilename(), 'info' => $info->getInfo(), 'ext' => $info->getExtension()];
}
else {
return ['code' => 0, 'msg' => $file->getError()];
}
}
}
~~~
`
> 保存文件和驗證文件
`
~~~
/**
* @param $md5
* @param $sha1
* @param $type
* @return array|bool
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
* @author: hhygyl <hhygyl520@qq.com>
* @name: checkFileIsUp
* @describe:根據文件MD5和哈希值判斷文件是否上傳過
*/
public function checkFileIsUp($md5, $sha1, $type)
{
$file_data = $this->where(['file_md5' => $md5, 'file_sha1' => $sha1, 'up_type' => $type])->find();
if(empty($file_data)) {
return true;
}
else {
return ['path' => $file_data['file_path'], 'info' => ['name' => $file_data['filename'], 'size' => $file_data['file_size'],'save_id'=>$file_data['id'], 'type' => $file_data['suffix']]];
}
}
/**
* @param $arr
* @return int|string
* @author: hhygyl <hhygyl520@qq.com>
* @name: saveUpload
* @describe:保存到表里
*/
public function saveUpload($arr)
{
return $this->insertGetId($arr);
}
~~~
`
- 空白目錄
- thinkphp5
- tools-常用類庫
- redis類庫
- Excel類庫
- File文件操作類庫
- Http請求類庫
- Maile郵件發送
- Hooks行為鉤子
- 七牛云
- 隨機數和字符串生成
- 字符串處理
- 時間類處理
- tree型轉換
- 工具類庫
- 文件打包下載
- 常用功能
- 文件上傳
- php生成word文檔
- elasticsearch 基本搜索
- 使用jwt開發API接口
- 安裝模及搭建
- ApiCheck.php
- ApiCheckLogin.php
- common.php
- Login.php
- Comment.php
- 漢字轉拼音
- 安裝使用
- Pinyin類
- elasticsearch操作
- 常用方法
- 數據源生成layui-select
- 獲取自定義配置項
- 百度編輯器
- 格式化文件大小
- 多語言設置
- hook監聽
- 域名綁定到模塊
- thinkphp6
- 文件上傳
- tp5totp6
- 創建路徑
- 獲取類所有方法
- password_hash加密驗證
- 生成 qrcode
- 郵件發送
- 獲取QQ信息
- GoogleAuthenticator
- redis限流
- redis 加鎖
- 百度翻譯
- QueryList爬取數據
- 獲取時間類
- 命令
- Git常用命令
- easyswoole
- pix_qrcode
- 驗證 cpf,cnpj
- php常用方法
- 日志
- 卡通頭像
- 兩位小數
- 圖片轉base64
- auth加密解密
- phpoffice/phpspreadsheet導入導出
- fastadmin
- 樹結構
- 單選框
- 復選框
- 二級搜索
- select選擇框
- selectpage選中回調事件
- 標簽添加
- 修改where條件
- 表格列表中添加input框
- selectpage事件
- fieldlist
- js操作
- test_js
- 多表格
- template模板