Laravel 4:在创建新文件时调用成员函数move()


Laravel 4 : Call to a member function move() on a non-object with when creating a new file

我有一个webb应用程序,用户填写表格,并根据该条目一个新的文件必须创建。

下面是mycontroller中存储函数的代码:

public function store()
{
    $input = Input::only('name', 'location', 'description', 'phrase');
    $this->homepageForm->validate($input);
    $homoepage = Homepage::create($input);
    $homoepage->save();
    $name = Input::get('name');
    $destination = app_path()."/views/uploads";
    $ext = "blade.php";
    $filename = str_random(6).'.'.$ext;
   $file = $name->move($destination, $filename);
    if($file)
   {
       echo 'good';
   }
  //return Redirect::home();
}

这是表单中的一个输入字段,用户将在其中输入位置的名称

   $name = Input::get('name');

,此名称将用作扩展名的文件名。我想创建这个文件并保存到上传文件夹中。

文件名和目标是正确的。我已经证实了。但当我想把它移到上传文件夹时,它就会抛出一个错误。谢谢! !

要解决这个问题,您需要:

Form::open(array('url' => 'foo/bar', 'files' => true))

<form method="post" action="{{ URL::route('your-route') }}" 
enctype="multipart/form-data">

replace $name = Input::get('name'); By:

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

您现在没有创建文件…为什么不直接在上传文件夹中创建它呢?

// added a / at the end here:
$destination = app_path()."/views/uploads/";
$ext = "blade.php";
$file = $destination . str_random(6).'.'.$ext;
// creates an empty file
$f = fopen($file, 'w');
fclose($f);
if(File::exists($file))
{
    echo 'good';
}

不确定你是否考虑过这一点,但你正在创建这个文件与一个随机的名称…你把这个名字记在什么地方了吗?如果没有,以后可能很难找到!!:)