Forum Moderators: coopster
I have a form where user can send up to three pictures.
<input type="file" name="imagefile[]">
<input type="file" name="imagefile[]">
<input type="file" name="imagefile[]">, etc...
Now, this what's happening when someone post two pictures but escape the second input (that is use the 1st and 3rd input).
I get this (you can see that [1] is empty) :
Array (
[name]=>Array([0]=>photo.jpg [1]=> [2]=>photo2.jpg)
[type]=>Array([0]=>image/jpeg [1]=> [2]=>image/jpeg)
[tmp_name]=>Array([0]=>D:\3F6.tmp [1]=> [2]=>D:\3F9.tmp)
[error]=>Array([0]=>0 [1]=>4 [2]=>0)
[size]=>Array([0]=>24426 [1]=>0 [2]=>28712)
)
Is there a way to rearrange the array so that empty key are at the end of the array (I need this: [name]=>Array([0]=>photo.jpg [1]=>photo2.jpg [2]=> )
Thanks
pseudocode (off the top of my head):
for($i=0; $i<count($_FILES); $i++)){
if ( isset($_FILES['name'][$i]) && ($_FILES['name'][$i]!='') ) {
// do your processing
}
}
Indeed, this is what I did yesterday (use foreach and remove empty value) and I got this:
Array (
[name]=>Array([0]=>photo.jpg [2]=>photo2.jpg)
[type]=>Array([0]=>image/jpeg [2]=>image/jpeg)
[tmp_name]=>Array([0]=>D:\3F6.tmp [2]=>D:\3F9.tmp)
[error]=>Array([0]=>0 [2]=>0)
[size]=>Array([0]=>24426 [2]=>28712)
)
As you can see the key[1] has been removed.
I did some search yesterday on how I could reinitialise keys so that instead of having [0] & [2], I have [0] & [1].
<input type="file" name="imagefile[]">
$new_array = array();for ($i=0; $i<count($_FILES['imagefile']); $i++)
{if (!empty($_FILES['imagefile']['tmp_name'][$i]))
{
$new_array[0][$i] = $_FILES['imagefile']['tmp_name'][$i];
$new_array[1][$i] = $_FILES['imagefile']['name'][$i];
$new_array[2][$i] = $_FILES['imagefile']['type'][$i];
$new_array[3][$i] = $_FILES['imagefile']['size'][$i];
}}
Then work with the new array, $new_array.
for ($i=0; $i<count($new_array); $i++)
{
echo "Temp Name :" . $new_array[0][$i] . "<br>";
echo "Name :" . $new_array[1][$i] . "<br>";
echo "Type :" . $new_array[2][$i] . "<br>";
echo "Size :" . $new_array[3][$i] . "<br><br>";
}
Good luck.
dc
$new_array[0][] = $_FILES['imagefile']['tmp_name'][$i];
$new_array[1][] = $_FILES['imagefile']['name'][$i];
$new_array[2][] = $_FILES['imagefile']['type'][$i];
$new_array[3][] = $_FILES['imagefile']['size'][$i];
Otherwise, you're just building a new array that will have the same keys. (Since you are using the increment as the new key.)
My point was not to remove the empty key, but to just ignore the empty one. Why do you need the keys to be accurately numbered? If the problem is that a
count($_FILE['name'])isn't giving an accurate count, I'd just increment a counter inside my check for an empty array.
...on how I could reinitialise keys so that instead of having [0] & [2], I have [0] & [1].
array_values() is usually the ticket to get that done. However, this is a multidimensional array as you have noted so I think your best option will be a user defined function with a foreach loop as you have already done. A combination of the two will probably get you where you want to end up then. Something along these lines ...
function filter_FILES()
{
$blanksfound = false;
foreach ($_FILES['name'] as $k => $v) {
if (!$v) {
$blanksfound = true;
foreach ($_FILES as $fkey => $fval) {
unset($_FILES[$fkey][$k]);
}
}
}
if ($blanksfound) {
foreach ($_FILES as $fkey => $fval) {
$_FILES[$fkey] = array_values($_FILES[$fkey]);
}
}
}
Yeah, in fact, Whoisgregg and Coopster are right, what I was I trying to do is stupid because there is no use of getting rid off empty values... As you said, I can just process what is not empty!
Somehow, when you spend too much time on one thing, you tend to forget the basic and make things complicated :(
Again, thanks for your help and tips, much appreciated.
Tomda