2014-03-30 96 views
0

Helllo,你好在Laravel上传文件

我想在Laravel上传文件。但是,当我在控制器中时,无论试图联系对象的方法都会抱怨它不是这样的对象,并且它不能获得它的属性。

我有三个字段,类别,标题和内容,然后是文件本身。我已经在这里搜索,并有一些复杂的代码上传多个文件,然后类似于我的,但没有获得批准的答案。

我也不明白这个过程是怎么样的,我只是想象它。我的意思是,一种方法实际上可以将文件放置在服务器的文件夹中,但我也需要将其路径存储在数据库的表中。我不知道实际发生的情况是,我阅读了Laravel文档中的文档,这是我所知道的。

$category = Input::get('category'); 
$title = Input::get('title'); 
$content = Input::get('content'); 
$thefile = Input::file('fichero'); 
$filename = Input::file($thefile)->getClientOriginalName(); 
$destinationPath = "etc etc://../laravel/cosas"; 
Input::file('fichero')->move($destinationPath, $fileName); 
$path = Input::file('fichero')->getRealPath(); 

那么这是我应该发送给模型(只是凭直觉,以前从来没有的话)

(new document)->insertDocs($category, $title, $content, $path); 

有谁知道如何做到这一点?

非常感谢你

回答

0

在你的控制器:

public function store() 
{ 
    // Upload file 
    $file = Input::file('fichero'); 
    $destinationPath = 'uploads/folder'; 
    $file->move($destinationPath); 

    // Create model 
    $model = new Model; 
    $model->category = Input::get('category'); 
    $model->title = Input::get('title'); 
    $model->content = Input::get('content'); 
    $model->filename = $file->getClientOriginalName(); 
    $model->destinationPath = $destinationPath; 

    // Save model 
    $model->save(); 
} 
+0

你好谢谢。请原谅我可怕的无知,我很努力地学习,很快。你写的是放在控制器中?那么,模型本身没有编写代码?我不需要模型的PHP文件? –

+0

我会给你正确的答案,并会在稍后尝试。 –

+0

在控制器中是。 bedankt,merci bien! –