0

I am building a image upload in Laravel but I keep getting an error inside my foreach loop if one filed is empty.

My upload allows multible images[], so if one field is empty I get an error but I want to allow users to choose if they want to upload eg 2 or 5 images

$input = Input::all();

//Validation

File::exists($path) or File::makeDirectory($path);
foreach($input['images'] as $file) {

    $image = Image::make($file->getRealPath()); //getRealPath gives me an error if not all images[] fields from post data containts an image

}

So how can I sort my input images[] from empty inputs?

Thanks in advance,

2
  • I added this inside my forach loop. if($file == ""){ break; } . Now it works by skipping over and loop again. Is this solution good? Commented Jan 21, 2015 at 18:46
  • Please edit your question to show your changes, rather than posting them in comments. Commented Jan 21, 2015 at 19:03

4 Answers 4

1

If I understood well you need something like this:
if(empty($file)) { unset($file); }

or something like this:

if(!empty($file)){
 $image = Image::make($file->getRealPath());
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try checking whether $file has a non-null value before running getRealPath

File::exists($path) or File::makeDirectory($path);
foreach($input['images'] as $file) {
    if($file) {
      $image = Image::make($file->getRealPath());
    }
}

By the way, $image is reset with every iteration. Is that really what you want? Do you care which $image you get?

Comments

0

You can use array_filter() with no callback to remove all elements that have a falsy value:

$input = array_filter($input);
foreach($input['images'] as $file) {
    $image = Image::make($file->getRealPath());
}

Comments

0

What I did to solve it was to add if check within my foreach loop and it worked. But im not sure if this is the best solution?

$input = Input::all();
foreach($input['images'] as $file) {

    if($file == ""){  //If one input is empty it jumps over it, instead of trying to use getRealPath() on an empty value
            break;
    }

    $image = Image::make($file->getRealPath()); 

}

3 Comments

First thing: you should update your question instead of posting an answer to it. Second thing: sometimes $file can be NULL and will never enter your condition. Try to use one of the answers that users already gave you above. And accept one of it. P.s. empty() function checks against empty string, 0 , null, false etc.. so I prefer to take advantage of it as much as I can.
@Dianna noted, and I will do so. Off topic, do you know how I can validate the total number of images uploaded in Laravel? Eg atleast require 1 image, and at most 5 images.
There are more ways to do this. One, for example, is to make your validation in the backend(php), and there you can check if count($images) <1 or count($images) >5 => return error validation.For example: return Redirect::back()->withErrors($validation); But I recommend you read more about validation in laravel here -> laravel.com/docs/4.2/validation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.