Jump to content

reverse file upload order


dj-kenpo

Recommended Posts

I'm for some reason not able to reverse the array of files I'm uplading. I have a form for uploading 4 images at once. becuase it's javascript on the form, it adds a newfile browse and places the previous one below. that means though when you hit upload, the first file you selected is the last, and vice veras.

 

on the process end I'm suing this

 

$photos_uploaded = $_FILES['photo_filename'];
$photos_uploaded = array_reverse($photos_uploaded, true);

 

true, makes no difference, and array_reverse makes no difference, I always get the last file first. for a sequential photo album, this is no good.

 

can I not use array reverse on the array 'photo_filename' ? all 4 upload input slots are named 'photo_filename', so it should be a normal array of 'stuff' right?

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/53949-reverse-file-upload-order/
Share on other sites

print_r($_FILES);

 

What do you get? Is it even being passed?

 

You can use the same variable name.

 

I tested it with an array and it worked fine

 

<?php
$array = array('hello','goodbye','hi');
$x=1;
foreach($array as $arra){
echo "$x. $arra<br>\n";
$x++;
}
echo "<br>\n";
$array = array_reverse($array,true);
$y=1;
foreach($array as $arra){
echo "$y. $arra<br>\n";
$y++;
}
?>

Make the input slot:

 

photo_filename[]

 

$files = $_FILES['photo_filename'];
$filenames = $_FILES['photo_filename']['name'];

$filenames = array_reverse($filenames,true);

$x=1;
foreach($filenames as $file){
echo "$x. $file<br>\n";
$x++;
}

 

See if they go in reverse order here, but don't allow upload atm.

the input was already photo_filename[] so no change needed there,

 

when I placed this above the files, it outputted in the correct order ie

 

1. IMG_0427.JPG

2. IMG_0428.JPG

3. IMG_0430.JPG

4.

 

(with the normal script below in reverse order.

 

the problem though is there's an extra blank one now for some reason. #4. it didn't seem to detect the size of the array correctly.

 

I guess I could just add count($files) with a while loop to check, but that seems like a bandaid it would be better to have the array the correct size in the firs tplace.

 

i'm tired after a long day so the answer is probably right there, I have no idea though, any thoughts?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.