php Laravel 4 上传图片表单

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

Laravel 4 upload image form

phpframeworkslaravellaravel-4

提问by Ruben

I'm trying to make a form that allows uploading a file and store it in the public/upload folder of the application.

我正在尝试制作一个允许上传文件并将其存储在应用程序的 public/upload 文件夹中的表单。

my form is:

我的表格是:

<form action="/image/save" method="post">
<input name="image" type="file" />
<input type="submit" value="Save"/>
</form>

and the controller:

和控制器:

public function save(){
$file = Input::file('image');
$destinationPath = 'upload/';
$filename = $file->getClientOriginalName();
Input::file('image')->move($destinationPath, $filename);

But when I run it I get the following error: Call to a member function getClientOriginalName()on a non-object

但是当我运行它时,我收到以下错误:调用getClientOriginalName()非对象上的成员函数

回答by Ryan Tablada

You need to have a file inputenabled on a form. (Doc)

您需要在表单上启用文件输入。(文档)

In Laravel, you can use:

在 Laravel 中,您可以使用:

Form::open('image/save', array('files'=> true))

or

或者

<form action="/images/save" method="post" enctype="multipart/form-data">

Otherwise all you are receiving from the file input is the file name as a string.

否则,您从文件输入接收到的只是作为字符串的文件名。

回答by Cengkaruk

If you not use Blade for generate your form like this

如果您不使用 Blade 生成这样的表单

{{ Form::open(array('url' => 'foo/bar', 'files' => true)) }}

Just add this parameter to your form tag

只需将此参数添加到您的表单标签中

enctype="multipart/form-data"

回答by Abd Ul Aziz

I also faced the same problem with one of my forms, the problem was the form wasn't defined with the option, 'files' => true, which tells the Laravel4 form helper to define a form with enctype="multipart/form-data"

我的一个表单也遇到了同样的问题,问题是表单没有用选项定义,'files' => true,它告诉 Laravel4 表单助手定义一个表单enctype="multipart/form-data"

Here is what i did:

这是我所做的:

Define the following array in your controller and pass it to your view ,

在您的控制器中定义以下数组并将其传递给您的视图,

$form_options = array (
    'url' => 'path/to/defined/route',
    'class'  => 'form-horizontal',
    'name'   => 'newUser',
    'files'  => true
);

In your view, define your form as follows:

在您看来,请按如下方式定义您的表单:

{{ Form::open( $form_options ) }}

That will define your form with enctype="multipart/form-data"

这将定义您的表单 enctype="multipart/form-data"

回答by claudevandort

You have to get the filename as follows:

您必须按如下方式获取文件名:

//Blade form
{{ Form::open_for_files('/image/save') }}
{{ Form::file('image') }}
{{ Form::close() }}

//get imagename
$filename = Input::file('image.name');

Good luck! :)

祝你好运!:)

回答by TechnoSakil

You can just add the field 'files'=> truein your array()if you use Laravel form style something like

如果您使用 Laravel 表单样式,则可以'files'=> true在您的字段中添加该字段array()

{{ Form::open(array('url'=>'/images/save','role'=>'form','files'=> true)) }}

otherwise you can use the html form tag something like

否则你可以使用html表单标签之类的

<form action="/images/save" method="post" enctype="multipart/form-data">

Hope this will help you.

希望这会帮助你。