theUnforgiven's avatar

Pulling my hair out

I;m trying to upload an image or multiple images and keep getting Call to a member function getClientOriginalExtension() on a non-object

I have this in my controller:

if (Input::hasFile('images')){

            $file = Input::file('images');
            $name = str_random(12).'.'.$file->getClientOriginalExtension();

            $image = Image::make(Input::file('images')->getRealPath())->resize(200);
            $image->save(public_path() . '/uploads/profile/' . $name);
            $product->images = $name;

        }
        $product->save();

        return Redirect::to('products/list')->with('flash_success', 'Product successfully created.');

Can't think for the life of my why this is.

0 likes
21 replies
theUnforgiven's avatar

My form is as follows:

{{ Form::open(['route' => 'products.store', 'class' => 'form-horizontal', 'files' => true]) }}

                <div class="form-group">
                     <div class="col-sm-2">Customer</div>
                         <div class="col-lg-3">
                             <input type="text" name="customer" class="form-control">
                         </div>
                </div>

  <div class="form-group">
                     <div class="col-sm-2">Images</div>
                         <div class="col-lg-10">
                             <input type="file" name="images">
                         </div>
                </div>
{{ Form::close() }}
bashy's avatar

Why don't you try return the file details with plain PHP?

return var_dump($_FILES['images']);
theUnforgiven's avatar

I did and it returns the file:

array (size=5)
  'name' => 
    array (size=1)
      0 => string 'spritemap.png' (length=13)
  'type' => 
    array (size=1)
      0 => string 'image/png' (length=9)
  'tmp_name' => 
    array (size=1)
      0 => string '/tmp/phpfSHbOt' (length=14)
  'error' => 
    array (size=1)
      0 => int 0
  'size' => 
    array (size=1)
bashy's avatar

So the array of data is there, that's something :)

Try get one of those from array?

$_FILES['images']['name']
bashy's avatar

Well, these should be usable then

// Use a file that's been uploaded
 Input::file('filename');
// Determine if a file was uploaded
 Input::hasFile('filename');
// Access file properties
 Input::file('name')->getRealPath();
Input::file('name')->getClientOriginalName();
Input::file('name')->getClientOriginalExtension();
Input::file('name')->getSize();
Input::file('name')->getMimeType();
// Move an uploaded file
 Input::file('name')->move($destinationPath);
// Move an uploaded file
 Input::file('name')->move($destinationPath, $fileName);
theUnforgiven's avatar

Get extension is that something new in 4.2.8?

@bashy I already have a if statements at the top of this thread you will see input has file

Felix's avatar

I handled something similiar on the weekend, are you sure you don't submitting multiple files? (I received an array with the objects inside)

I had that issues, while using dropzone,js ...

theUnforgiven's avatar

I will be submitting multiple files yes so need then to store as an array or something in the DB once uploaded. I scrapped dropzone because of this.

theUnforgiven's avatar

By changing to $file->guessExtension(); that works but only uploads one image even though I have <input type="file" name="images[]" multiple>

theUnforgiven's avatar

Now this

$file->move(public_path() . '/uploads/products/' . $filename);

Is creating a folder and not moving it. Am I missing something here since the upgrade to 4.2.8

bashy's avatar

Putting files in the database as an array isn't the best practise, make a relationship table?

theUnforgiven's avatar

I just wanna use multiple uploads without dropzoneJS and have it stored in either an array or json

kreitje's avatar

If you have images[] for the input name you need to loop through the files.

foreach( Input::file('images') as $file) {

            $name = str_random(12).'.'.$file->getClientOriginalExtension();

            $image = Image::make( $file->getRealPath() )->resize(200);
            $image->save(public_path() . '/uploads/profile/' . $name);
            $product->images = $name;
}
kakirigi's avatar
Level 9

Then maybe move the persistence bit out of the foreach block? Something like this maybe:

$imagelist = []; 

foreach( Input::file('images') as $file) {
  $name = str_random(12).'.'.$file->getClientOriginalExtension();

  $image = Image::make( $file->getRealPath() )->resize(200);
  $image->save(public_path() . '/uploads/profile/' . $name);

  $imagelist[] = $name;
}

$imagelist = json_encode($imagelist);

$product->images = $imagelist;
theUnforgiven's avatar

Question is now how would I saw them from the json array in my table list

<img src="/uploads/profile/{{ $product->images }}">

??

theUnforgiven's avatar

AS you can probably guess JSON is still new to me having not worked with it a lot.

kakirigi's avatar

You can use json_decode($product->images) to get an array of your images. I don't know how you are displaying your images, but a presenter could turn the image names in the array into urls. You can then call them like $product->present()->images and use foreach in the view to display them all.

Also, it would be a better idea to use {{ URL::to('/uploads/profile/'. $image) }} than writing it plainly since this'll add your site's url to the link as well.

Please or to participate in this conversation.