简体   繁体   English

Laravel 5关于图像/文件上传的疑问

[英]Laravel 5 doubts about image/file upload

I'm creating a small institutional website for my company, and there is going to be a Blog(can only post from backoffice, only for admins). 我正在为我的公司创建一个小型机构网站,并且将会有一个Blog(只能从后台发布,仅适用于管理员)。 I'm having trouble basically with the image upload. 基本上我在上传图片时遇到了麻烦。 I need to store the image link in the database, but I don't know how to move the uploaded image to my resources folder. 我需要将图像链接存储在数据库中,但是我不知道如何将上传的图像移动到我的资源文件夹中。

This is my first time working with file upload in Laravel, and the documentation is kind of confusing with the Local Storage and Cloud Storage. 这是我第一次在Laravel中处理文件上传,并且该文档有点与本地存储和云存储混淆。 I'm not really sure which one I need to use... 我不确定我需要使用哪一个...

I only have this in my template: 我的模板中只有这个:

<label for="image" class="col-md-4 control-label">Upload image</label>
<div class="col-md-6">
    <input id="image" type="file" name="image" required>
</div>

I have no idea how to process this in the controller.. 我不知道如何在控制器中处理它。

EDIT: 编辑:

So, after reading more throughly the docs, I think I've understood. 因此,在通读更多文档之后,我想我已经了解了。 Here is what I've got so far: 这是到目前为止我得到的:

if ($request->hasFile('image') && $request->file('image')->isValid()) {
     $path = $request->photo->store('image');
}  

But now I'm getting this error: Call to a member function store() on null 但是现在我得到了这个错误: Call to a member function store() on null

I'm using the public drive, since everybody can see the blog posts. 我正在使用public驱动器,因为每个人都可以看到博客文章。 Any help? 有什么帮助吗?

尝试这个:

$path = $request->image->store('images');

You can use local storage, cloud storage is for example persisting to an Amazon S3 Bucket. 您可以使用本地存储,例如,云存储将持久存储到Amazon S3存储桶。

In your controller you can check for and retrieve files using 在您的控制器中,您可以使用以下命令检查和检索文件

if (Input::file('image'))
{
    // file is set.
    // Move the file.
   Input::file('image')->move($destinationPath, $fileName);
}

storing-uploaded-files 存储上传的文件

Storing Uploaded Files 存储上传的文件

To store an uploaded file, you will typically use one of your configured filesystems. 要存储上载的文件,通常将使用已配置的文件系统之一。 The UploadedFile class has a store method which will move an uploaded file to one of your disks, which may be a location on your local filesystem or even a cloud storage location like Amazon S3. UploadedFile类具有一个store方法,该方法会将上载的文件移动到您的一个磁盘上,该磁盘可以是本地文件系统上的某个位置,甚至可以是Amazon S3之类的云存储位置。

The store method accepts the path where the file should be stored relative to the filesystem's configured root directory. store方法接受相对于文件系统配置的根目录应存储文件的路径。 This path should not contain a file name, since a unique ID will automatically be generated to serve as the file name. 该路径不应包含文件名,因为将自动生成一个唯一的ID作为文件名。

The store method also accepts an optional second argument for the name of the disk that should be used to store the file. store方法还接受用于存储文件的磁盘名称的可选第二个参数。 The method will return the path of the file relative to the disk's root: 该方法将返回文件相对于磁盘根目录的路径:

$path = $request->photo->store('images');

$path = $request->photo->store('images', 's3');

If you do not want a file name to be automatically generated, you may use the storeAs method, which accepts the path, file name, and disk name as its arguments: 如果您不希望自动生成文件名,则可以使用storeAs方法,该方法接受路径,文件名和磁盘名作为其参数:

$path = $request->photo->storeAs('images', 'filename.jpg');

$path = $request->photo->storeAs('images', 'filename.jpg', 's3');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM