【问题标题】:laravel 4 upload a filelaravel 4 上传文件
【发布时间】:2013-07-22 19:45:46
【问题描述】:

我正在尝试制作一个表格来上传文件,一个excel文件, 这是我的html

<h2>Agregar Torneo</h2>
{{Form::open('admin/addtorneo', 'POST',array('files' => 'true', 'enctype' => "multipart/form-data"))}}

    {{Form::label('cvs', 'Archivo:')}}

    {{Form::file('cvs')}}

    {{Form::submit('Subir')}}

{{Form::close()}}

和php

    $file = Input::file('cvs');

    $destinationPath = 'uploads/'.Str::random(5);

    $filename = Input::file('cvs.name');

    $uploadSuccess = Input::file('cvs')->move($destinationPath, $filename);

    $new = new Torneo;

    $new->nombre = $filename;
    $new->dir = $destinationPath;

    $new->save();

    return "Torneo agregado <br> <a href='../admin'>Volver</a>";

但我不断得到

Call to a member function move() on a non-object

我尝试使用 $file->getClientOriginalName() 而不是 Input::file('cvs.name') 但我在非对象上调用成员函数 getClientOriginalName(),在我看来,表单不对,它没有正确接收文件

【问题讨论】:

  • 看起来 $filename 没有生成。将 $filename 更改为 'testname' 之类的东西(不是变量),看看是否能解决问题。本质上它看起来像 $filename = 'test';如果确实有效,则将文件名更改为: $filename = $file['name'];
  • 我不断收到对非对象上的成员函数 move() 的调用:/
  • 当你在html中查看表单时,你看到了什么?也许你没有设置一个“名字”只是 id。

标签: php upload laravel laravel-4


【解决方案1】:

只需调用一次 Input::file('cvs'),第二次就变成空对象了。

例子:

$file = Input::file('cvs');
$destinationPath = 'uploads/'.Str::random(5);
$file->move($destinationPath);

有效。

【讨论】:

    猜你喜欢
    • 2013-04-06
    • 2015-03-21
    • 2013-08-11
    • 2014-03-07
    • 2015-04-24
    • 1970-01-01
    • 1970-01-01
    • 2013-03-14
    • 2013-08-14
    相关资源
    最近更新 更多