Laravel "Error loading file. Call member function getClientOriginalName () for non-object"

all. I am trying to learn Laravel and I am working on uploading an image. I am getting the following error:

"Calling member function getClientOriginalName () on non-object"

I am using these packages:

"anahkiasen/former": "dev-master",
"intervention/image": "dev-master",
"intervention/imagecache": "2.*"

      

After using SO, I checked the following: DO NOT Contribute to the above error:

  • multipart / form-data in the form
  • Load data files
  • PHP.ini max_filesize is much larger than the size of this small test file

My form:

<form enctype="multipart/form-data" accept-charset="utf-8" class="form-horizontal" id="create_form" method="POST" action="/elements">

<div class="control-group"><label for="img[]" class="control-label">Upload Image</label><div class="controls"><input multiple="true" class="myclass" accept="image/gif|image/jpeg|image/png" id="img[]" type="file" name="img[]"></div></div>

<div class="form-actions"><input class="btn-large btn-primary btn" type="submit" value="Submit"> <input class="btn-large btn-inverse btn" type="reset" value="Reset"></div>

<input type="hidden" name="_token" value="B0AJ0Y5LMrMng6CsePeZfNSvRQ0KexowOGTK99Gm">
</form>

      

Code to generate the error:

$image = Input::file('img');
$filename = $image->getClientOriginalName();
print_($filename);

      

If I print out an object using:

print_r ($ images);

... then I get:

Array
(
    [0] => Symfony\Component\HttpFoundation\File\UploadedFile Object
        (
            [test:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 
            [originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => storageunit.jpg
            [mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image/jpeg
            [size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 8734
            [error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
            [pathName:SplFileInfo:private] => /tmp/php9AU1OE
            [fileName:SplFileInfo:private] => php9AU1OE
        )

)

      

This all looks right to me, so I'm stumped.

If anyone has any ideas on what to try next I would appreciate some help.

+3


source to share


3 answers


See what's printed. $image

is an array of objects, not an object. Try:



$filename = $image[0]->getClientOriginalName();

      

+6


source


Doh!

"file" not "files"



{{
  Former::file('img')
  ->label('Upload Image')
  ->class('myclass')
  ->accept('gif', 'jpg', 'png');
}}

      

+1


source


For me, the only problem was that the class file that is required for the file upload element was missing.

I added this code and it worked,

use Illuminate \ Http \ UploadedFile;

0


source







All Articles