Laravel 4 - 上传文件和SQL

时间:2014-02-10 15:02:46

标签: file upload laravel laravel-4

我正在创建一个需要上传文件的应用程序,特别是视频,我正在使用laravel来执行服务器端的php。

我的观点有一个表格

{{ Form::open(array('route' => 'testuploads.store', 'files' => true, 'enctype' => 'multipart/form-data')) }}
<ul>
    <li>
        {{ Form::label('title', 'Title:') }}
        {{ Form::text('title') }}
    </li>

    <li>
        {{ Form::label('body', 'Body:') }}
        {{ Form::textarea('body') }}
    </li>

    <li>
        {{ Form::token() }}
        {{ Form::label('file', 'File:') }}
        {{ Form::file('file') }}
    </li>

    <li>
        {{ Form::submit('Submit', array('class' => 'btn btn-info')) }}
    </li>
</ul>
{{ Form::close() }}

控制器

public function store()
{
    $input = Input::all();

    $file = Input::file('file');
    $pubpath = public_path();
    $destinationPath = $pubpath.'/uploads/'.str_random(8);
    $filename = $file->getClientOriginalName();
    $extension = $file->getClientOriginalExtension();
    // $path = $file->getRealPath();
    $size = $file->getSize();
    $mime = $file->getMimeType();
    $uploadSuccess = Input::file('file')->move($destinationPath, $filename);

    $validation = Validator::make($input, Testupload::$rules);

    if ($validation->passes())
    {
        $this->testupload->create($input);
        return Redirect::route('testuploads.index');
    }

    return Redirect::route('testuploads.create')
        ->withInput()
        ->withErrors($validation)
        ->with('message', 'There were validation errors.');
}

我得到的错误是:

Symfony \ Component \ HttpFoundation \ File \ Exception \ FileException
Unable to create the "/www/test/public/uploads/xTzlVlxb" directory

我也试图将权限更改为上传目录,但这没有用。任何帮助表示感谢,谢谢。

1 个答案:

答案 0 :(得分:2)

尝试使用此方法首先创建文件夹:

 File::makeDirectory($destinationPath,  $mode = 0777, $recursive = false);

然后将文件移动到该文件夹​​中。另外,我建议首先检查文件夹是否成功创建:

 File::exists($destinationPath);