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

StackOverflow https://stackoverflow.com/questions/23627780

  •  21-07-2023
  •  | 
  •  

Вопрос

I have an webb app where user fills up the form and based upon that entry a new file has to be created.

Here are the code for store function in 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();
}

This is one of the input field in the form where user will enter a name of the location

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

and this name will be used as a file name with extension. And I want to create this file and save into uploads folder.

The file name and destination are correct. I have verified it. But when I want to move it to uploads folder, it just throws an error. Thanks!!

Это было полезно?

Решение 3

You're not creating the file at the moment... why not just create it straight into the uploads folder?

// 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';
}

Not sure if you have considered this, but you are creating this file with a random name... are you recording that name somewhere? might be hard to find later if not!! :)

Другие советы

To fix this you need to:

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

Or

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

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

$name = Input::file('name');
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top