0

I have a HTML form with multiple file inputs

<input type='file' id='images' name='images[]'>

I use implode() to covert the $images array into comma separated string and insert it into my DB , then when calling I use explode() to recall single images.

the problem is : empty input fields are still submitted to the database , so when exploded, I get empty array values

how can I get rid of these empty array values ? maybe even before using implode function ?

I'm thinking of running a foreach loop throw the $_FILES['images'] array and discarding empty values from it and rebuilding a new array .. but is any easier method available ?

thanks

1
  • write more details about your program. [ your submit form. and code php code that handle these images ] Commented Nov 4, 2013 at 20:06

2 Answers 2

2

Use array_filter. It is a function which iterates over an array with another function specified as an argument. But if you don't pass this argument, the array_filter will simply get rid of the values equal fo FALSE.

$images = implode(',', array_filter($_FILES['images']));
Sign up to request clarification or add additional context in comments.

2 Comments

Dont forget that array_filter also removes (int) 0 if you use it without a callback function, so consider using something like an anonymous function to make sure its empty and not (int) 0 (aka == false)
Just what I was looking for :)
1

$images = array_filter($_POST['images']);

But I would rethink the database design. You probably shouldn't be storing lists like that.

Comments

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.