1. 程式人生 > >laravel檔案上傳

laravel檔案上傳

        $file = Request::file('photo');//檔案
        $name = input::get('name');//其他屬性值
        $allowed_extensions = ["png", "jpg", "gif"];
        if ($file->getClientOriginalExtension() && !in_array($file->getClientOriginalExtension(), $allowed_extensions)) {
            return ['error' => 'You may only upload png, jpg or gif.'];
        }
        
$destinationPath = 'storage/uploads/'; //public 資料夾下面建 storage/uploads 資料夾 $extension = $file->getClientOriginalExtension(); $fileName = str_random(10).'.'.$extension; $file->move($destinationPath, $fileName); $filePath = asset($destinationPath.$fileName); $info
=DB::insert('insert into photo(pname,photo) VALUES (?,?)',[$name,$filePath]);//入庫 if($info){ return Redirect('/show'); }else{ echo "no"; }