我还是 Laravel 的新手。请帮我解决我的问题。我坚持不让公众看到文件。我试图在 Laravel 7 中将文件从公共访问变为私有。
我将所有链接都放在了 auth 中,它使所有链接在使用之前都需要访问。我试图访问一些链接来创建记录。我做不到,系统需要身份验证,但是当我使用 URL http://127.0.0.1:8000/storage/actions/filename.something没有登录来访问图像文件时。我仍然可以看到文件。
这是我的路线
Route::group(['prefix' => 'admin', 'middleware'=> ['auth', 'administrator']], function () {
//Action
Route::get('/addAction', 'admin\ActionController@create');
Route::get('/deleteAction/{id}', 'admin\ActionController@delete');
Route::get('editAction/{id}', 'admin\ActionController@edit');
Route::post('/addAction', 'admin\ActionController@store');
Route::post('/updateAction/{id}', 'admin\ActionController@update');
Route::get('/allAction', 'admin\ActionController@index');
Route::get('/delete/actionImage/{id}', 'admin\ActionController@deleteimages');
});
这是我用来将数据存储到数据库并将图像存储到文件路径的控制器。
public function index(){
return view('admin.allAction')
->with('actions',Action::all())
->with('challenges',Challenge::all())
->with('users',User::all())
->with('status',Status::all())
->with('images',Images::all());
}
public function create(){
$users = DB::table('users')
->where('role','2')
->get();
return view('admin.addAction')
->with('users', $users)
->with('status',Status::all());
public function store(Request $request){
$request->validate([
'name' => 'required|string|max:188',
'objective' => 'string|max:888',
'images' => 'required',
'images.*' => 'file|image|mimes:jpeg,png,jpg|max:8000',
]);
// Insert Data to Table
$action=new Action();
$action->name=$request->name;
$action->objective=$request->objective;
$action->status_id=$request->status_id;
$action->owner_id=$request->owner_id;
$action->challenge_id=$request->challenge_id;
/*dd($action);*/
$action->save();
if ($request->hasfile('images')) {
$images = $request->file('images');
foreach($images as $image) {
$name = time().'-'.$image->getClientOriginalName();
$name = str_replace(' ','-',$name);
/*$path =*/ $image->storeAs('actions', $name, 'public');
Images::insert([ /*OrUpdate*/
'name' => $name,
'action_id' => $action->id,
]);
}
}
Session()->flash("success", "Success!");
return redirect('/admin/addAction');
我的文件系统.php
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
'permissions' => [
'file' => [
'public' => 0664,
'private' => 0600,
],
'dir' => [
'public' => 0775,
'private' => 0700,
],
],
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
],
],
在我不知道该怎么做之前,我从不做私人文件。我试图在互联网上搜索如何做,但我仍然无法实现。
提前感谢您的所有评论,并对我的英语感到抱歉。