thinkphp 圖片上傳驗證
阿新 • • 發佈:2018-12-03
thinkphp中已經有對圖片上傳做了很好的驗證 使用如下:
驗證器類中寫:
namespace app\index\validate; use think\Validate; class User extends Validate { protected $rule = [ 'test' => ['require', 'image' => '225,225', 'fileExt' => 'png,jpg', 'fileSize' => 6500], ]; protected $message = [ 'test.image' => '不是225,225的圖形', 'test.fileExt' => '圖片的格式不符合要求', 'test.fileSize' => '內容不能大於6500位元組', ]; protected $scene = [ 'edit' => ['test'](這裡是場景) ];
控制器中驗證:
public function trr(Request $request) { //獲取上傳檔案 $file = $request->file('image'); //先驗證是否滿足條件 $result = $this->validate(['test' => $file], 'User.edit'); if (true === $result) { //滿足條件再做業務邏輯 $info = $file->move(ROOT_PATH.'public'.DS.'uploads'); } else { dump($result); } }