PHP–ThinkPHP6.0上傳文件配置
博客說明
文章所涉及的資料來自互聯網整理和個人總結,意在於個人學習和經驗彙總,如有什麼地方侵權,請聯繫本人刪除,謝謝!
說明
TP6.0多了一個filesystem.php的配置文件,是用來作爲上傳文件配置的
看雲文檔地址
https://www.kancloud.cn/manual/thinkphp6_0/1037639
前端代碼
<form action="/index/upload" enctype="multipart/form-data" method="post">
<input type="file" name="brand_img" /> <br>
<input type="submit" value="上傳" />
</form>
控制器
public function upload(){
// 獲取表單上傳文件 例如上傳了001.jpg
$file = request()->file('brand_img');
// 上傳到本地服務器
$fileName = Filesystem::putFile( 'topic', $file);
return $fileName;
}
配置文件
<?php
return [
// 默認磁盤
'default' => env('filesystem.driver', 'public'),
// 磁盤列表
'disks' => [
'local' => [
'type' => 'local',
'root' => app()->getRuntimePath() . 'storage',
],
'public' => [
// 磁盤類型
'type' => 'local',
// 磁盤路徑
'root' => app()->getRootPath() . 'public/uploads',
// 磁盤路徑對應的外部URL路徑
'url' => '/storage',
// 可見性
'visibility' => 'public',
],
// 更多的磁盤配置信息
],
];
可以自己選擇local還是public
我把完整代碼發一下
public function add(){
if(request()->isPost()){
$data = input('post.');
//圖片上傳
if($_FILES['brand_img']['tmp_name']){
$data['brand_img'] = $this->upload();
}
$add = Db::name('brand')->insert($data);
if($add){
$this->success('添加品牌成功','list');
}else{
$this->error('添加品牌失敗');
}
return;
}
return view();
}
public function upload(){
// 獲取表單上傳文件 例如上傳了001.jpg
$file = request()->file('brand_img');
// 上傳到本地服務器
$fileName = Filesystem::putFile( 'topic', $file);
return $fileName;
}
測試
發現在相應的目錄下已經上傳了圖片
數據庫裏面出現的圖片路徑
感謝
ThinkPHP
以及勤勞的自己