简体   繁体   English

laravel 5.3 上传文件

[英]laravel 5.3 upload file

I'm using laravel 5.3, and i want to upload a file to a directory.我正在使用 laravel 5.3,我想将文件上传到目录。 i've been following laravel documentation for uploading file, like this code :我一直在关注用于上传文件的 Laravel 文档,例如以下代码:

public function store(Request $request)
{
    $path = $request->attachment->store('attachment');
}

but it get an error但它得到一个错误

Call to a member function store() on string在字符串上调用成员函数 store()

previously on laravel 5.2, i've been use this code for uploading file and it's work以前在 Laravel 5.2 上,我一直在使用此代码上传文件,并且可以正常工作

if ($request->hasFile('attachment')) {
    $destination = 'upload/attachment';
    $file = $request->file('attachment');
    $file->move($destination, $file->getClientOriginalName());
}

$filelocation = $destination. "/" .$file->getClientOriginalName();

but in laravel 5.3 it's doesn't work, and i get an error但在 laravel 5.3 中它不起作用,我收到一个错误

Undefined variable: destination未定义变量:目的地

can you help what wrong with my code ?你能帮我的代码有什么问题吗?

public function store(Request $request)
{
    $path = $request->attachment->store('attachment');
}

I don't think $request->attachment does what you are trying to do.我不认为$request->attachment做你想做的事情。 Change that line to:将该行更改为:

$request->file('attachment')->store('/your/destination/path')

if($request->hasFile('attachment')){
    $destination = 'upload/attachment';
    $file = $request->file('attachment');
    $file->move($destination, $file->getClientOriginalName());
}

$filelocation = $destination. "/" .$file->getClientOriginalName();

When $request->hasFile('attachment') is false , $destination will not get declared.$request->hasFile('attachment')false$destination不会被声明。 So when it reaches the $filelocation line you get an undefined variable.因此,当它到达$filelocation行时,您会得到一个未定义的变量。

In Larave 5.3 you can use simple upload file like this:在 Larave 5.3 中,您可以像这样使用简单的上传文件:

$request->file('file')->storePublicly($folderSrc);

This save you file to storage/app/public/you-folder-src这会将您的文件保存到 storage/app/public/you-folder-src

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

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