2014-02-10 51 views
0

我正在創建一個應用程序,需要上傳文件,特別是視頻,我使用laravel來做服務器端的php。Laravel 4 - 上傳文件和SQL

我有一個表格上我的看法

{{ 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

'/ www/test/public/uploads /'可寫嗎? – JackPoint

回答

2

嘗試用這種方法首先創建的文件夾:

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

然後移動文件到該文件夾​​。另外我建議首先檢查文件夾是否成功創建:

File::exists($destinationPath); 
+0

嗨MaHo,感謝您的快速回復。這也沒有奏效,錯誤更改爲 ErrorException mkdir():權限被拒絕 – cch

+0

我看到了你自己的答案,你重命名了你的文件夾。你確定第一個文件夾'上傳'有正確的權限嗎?因爲聽起來有點奇怪,它使用不同的名稱。 – mauricehofman

+0

我原來改變了權限,並檢查是否有正確的權限,但沒有奏效,並且是奇怪的。無論如何,它現在的工作。 – cch