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
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++;
}
?>

Link to comment
Share on other sites

oh ya, the files are passed/uploaded/processed. everything works, but they go in reverse order....

 

are you sure $_FILES is treated exactly the same as a normal array? that's the only thing I can th9ink of as to why it wouldn't work.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.