需要帮助使用Laravel将file.DAT导入public / uploads文件夹

时间:2014-02-06 07:11:30

标签: php laravel-4

我正在尝试在public / uploads文件夹中导入file.DAT。 但它没有成功。错误显示为

  

在非对象上调用成员函数getClientOriginalName()。

这是我的代码

控制器

<?php

class ImportController extends BaseController {

    /*
    |--------------------------------------------------------------------------
    | Default Import Controller
    |--------------------------------------------------------------------------
    |
    | You may wish to use controllers instead of, or in addition to, Closure
    | based routes. That's great! Here is an example controller method to
    | get you started. To route to this controller, just add the route:
    |
    |   Route::get('/', 'ImportController@index');
    |
    */

    public function showImport()
    {
        return View::make('import');
    }

    public function handleImport()
    {
        $file = Input::file('file'); // Your file upload input field in the form should be named 'file'

        $destinationPath = 'public/uploads/'.str_random(8);
        $filename = $file->getClientOriginalName();
        $uploadSuccess = $file->move($destinationPath, $filename);

        if( $uploadSuccess ) {
            return Response::json('success', 200); // Or do a redirect with some message that file was uploaded
        } else {
            return Response::json('error', 400);
        }
    }
}

查看

<form action="{{ action('ImportController@handleImport') }}" method="post" role="form">
    <div class="form-group">
        <label for="inputFile">File Import</label>
        <input type="file" id="inputFile">
    </div>
    <input type="submit" value="Import" class="btn btn-primary" />
</form>

我的代码出了什么问题?

1 个答案:

答案 0 :(得分:0)

根据您的版本,Input::file('file');可以返回不同的类型。 使用dd($file)查看$ file是什么,根据您可以检查它是否是要继续的文件。