Laravel 不存储请求中的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42169126/
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-09-14 15:18:25  来源:igfitidea点击:

Laravel not storing files from request

phplaravellaravel-5

提问by Michael

I got form with file upload named "image". Form is working and i'm getting image but for some reason it's not stored. this is the code I use to store image:

我得到了名为“图像”的文件上传表单。表单正在工作,我正在获取图像,但由于某种原因它没有被存储。这是我用来存储图像的代码:

$path = $request->image->store('storage/uploads');

before that I check

在此之前我检查

$request->hasFile('image')

and later I'm saving $path. Path is successfully saved and when i check it it's really storage/uploads/radnomid but there is no file

后来我保存了 $path。路径已成功保存,当我检查它时,它确实是 st​​orage/uploads/radnomid 但没有文件

采纳答案by Michael

Sorry for posting my own answer but problem was that by default laravel public disk does not upload to public directory. Settings needed to be changed like this: config/filesystems.php

很抱歉发布我自己的答案,但问题是默认情况下 Laravel 公共磁盘不会上传到公共目录。需要像这样更改设置:config/filesystems.php

  'public' => [
              'driver'     => 'local',
              'root'       => public_path(),
              'visibility' => 'public',
          ],

and then simply:

然后简单地:

$path = $request->image->store('storage/uploads','public');

回答by Bogdan Matkovsky

If your filesystems.php configs were default then

如果您的 filesystems.php 配置是默认的,那么

$path = $request->image->store('storage/uploads');

saved your files not in storage/uploads but in storage/app/storage/uploads.

将您的文件保存在 storage/app/storage/uploads 中,而不是存储/上传中。

And your current accepted answer is not really good because public_path() intended to be used for storing template files not user uploads. According to official docs uploads should be used in storage/ folder with a symbolic link from public/storage/ but not in public/.

而且您当前接受的答案并不是很好,因为 public_path() 旨在用于存储模板文件而不是用户上传。根据官方文档,上传应该在带有来自 public/storage/ 的符号链接的 storage/ 文件夹中使用,但不能在 public/ 中使用。

Read more in https://laravel.com/docs/5.4/filesystem#the-public-disk

https://laravel.com/docs/5.4/filesystem#the-public-disk 中阅读更多内容

回答by Brandon Baah

Make sure that you are including enctype of multipart/form-data. You can either add the attribute directly to your form, or you can add a 'files' => true if you are using the FORM helper.

确保您包含 multipart/form-data 的 enctype。您可以将属性直接添加到表单中,或者如果您正在使用 FORM 帮助程序,则可以添加 'files' => true。

This is the conventional way to use the upload function that comes with Laravel 5.

这是使用 Laravel 5 自带的上传功能的常规方式。

public function processForm(Request $request) 
{
    if( $request->hasFile('image') ) {
        $file = $request->file('image');
        // Now you have your file in a variable that you can do things with
    }
}

回答by Rid Islam

you can try with this code,

你可以试试这个代码,

Image::make($request->file('image'))->save('upload_path/filename.jpg'));

here is complete doc of this Image package. http://image.intervention.io/

这是此图像包的完整文档。 http://image.intervention.io/

回答by Advaith

Try doing like this

尝试这样做

if ($request->hasFile('image')) {
   $image = $request->file('image');
   $filename = time() . '.' . $image->getClientOriginalExtension();
   $location = public_path('images/comment/' . $filename);
   Image::make($image)->resize(800, 600)->save($location);
}

回答by AddWeb Solution Pvt Ltd

I think you should try this:

我认为你应该试试这个:

Image::make($request->file('image'))->save(storage_path('uploads/image.jpg')); 

Hope this works for you!

希望这对你有用!