php 如何将 Laravel 中的文件直接上传到公共文件夹?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44577380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:00:35  来源:igfitidea点击:

How to upload files in Laravel directly into public folder?

phplaravellaravel-5.4

提问by Fabiotk

The server which I'm hosting my website does not support linksso I cannot run the php artisan storage:linkto link my storage directory into the public directory. I tried to remake the disk configuration in the filesystems.phpto directly reference the publicfolder but it didn't seems to work either. Is there a way to upload a file using Laravel libraries directly into the public folder or will I have to use a php method? Thanks in advance.

我托管网站的服务器不支持,links因此我无法运行 将php artisan storage:link我的存储目录链接到公共目录。我试图在filesystems.php 中重新制作磁盘配置以直接引用该public文件夹,但它似乎也不起作用。有没有办法使用 Laravel 库直接将文件上传到公共文件夹中,或者我必须使用 php 方法?提前致谢。

回答by sunomad

You can create a new storage disc in config/filesystems.php:

您可以在以下位置创建新的存储光盘config/filesystems.php

'public_uploads' => [
    'driver' => 'local',
    'root'   => public_path() . '/uploads',
],

And store files like this:

并像这样存储文件:

if(!Storage::disk('public_uploads')->put($path, $file_content)) {
    return false;
}

回答by Inoyatulloh

You can pass disk to method of \Illuminate\Http\UploadedFileclass:

您可以将磁盘传递给\Illuminate\Http\UploadedFile类的方法:

$file = request()->file('uploadFile');
$file->store('toPath', ['disk' => 'public']);

or you can create new Filesystem disk and you can save it to that disk.

或者您可以创建新的文件系统磁盘并将其保存到该磁盘。

You can create a new storage disk in config/filesystems.php:

您可以在以下位置创建新的存储磁盘config/filesystems.php

'my_files' => [
    'driver' => 'local',
    'root'   => public_path() . '/myfiles',
],

in controller:

在控制器中:

$file = request()->file('uploadFile');
$file->store('toPath', ['disk' => 'my_files']);

回答by Willie Mwewa

You should try this hopping you have added method="post" enctype="multipart/form-data" to your form. Note that the public path (.uploadedimages) will be moved to will be your public folder in your project directory and thats where the uploaded image will be.

您应该尝试在表单中添加 method="post" enctype="multipart/form-data" 的这种跳跃。请注意,公共路径 (.uploadedimages) 将被移动到您项目目录中的公共文件夹,这就是上传图像的位置。

public function store (Request $request) {

  imageName = time().'.'.$request->image->getClientOriginalExtension();
  $request->image->move(public_path('/uploadedimages'), $imageName);

  // then you can save $imageName to the database

}

回答by Bart?omiej Sobieszek

In addition to every answer there, I would suggest to add another protection layer because it's a public folder.

除了那里的每个答案之外,我还建议添加另一个保护层,因为它是一个公共文件夹。

For every file that you will have in your public folder that requires protection, do a route that will verify access to them, like

对于需要保护的公用文件夹中的每个文件,执行一个路由来验证对它们的访问,例如

/files/images/cat.jpg

/files/images/cat.jpg

and route that looks like /files/images/{image_name}so you could verify the user against given file.

和路由看起来像/files/images/{image_name}这样你可以根据给定的文件验证用户。

After a correct validation you just do return response($full_server_filepath, 200)->header('Content-Type', 'image/jpeg');

正确验证后,您只需执行 return response($full_server_filepath, 200)->header('Content-Type', 'image/jpeg');

It makes a work little harder, but much safer

它使工作更难,但更安全

回答by Agus

as alternative,
let laravel set the file stored in /storage/appfolder.

或者,
让 laravel 设置存储在/storage/app文件夹中的文件。

if we want to read the file, just use like $publicPath = '../storage/app/'.$path;

如果我们想读取文件,只需使用像 $publicPath = '../storage/app/'.$path;