0

I have this file input:

<input type="file" name="images[]" accept="image/*" multiple />

In my controller I upload them this way:

 $x = 0;
 foreach(Input::file('images') as $file) {

                    $filename = $x.'.'.$file->guessClientExtension();
                    //dest, name
                    if (!file_exists($thepath)) {
                        mkdir($thepath, 0777, true);
                    }
                    $uploadflag = $file->move($thepath,$filename);

                    $is_main = ($x==0) ? true : NULL; //Know who's first img

                    $img_submit = DB::table('images')->insert(array(
                        'image_id'          => $filename,
                        'is_main'           => $is_main
                    ));

                    if($uploadflag) {
                        $uploadedfiles[] = $filename;
                    }
                $x++;
 }

I have a plugin wich sorts them out the way I want, if I do a print_r to the form I get:

Array (
[title] => 
[images] => Array
    (
        [0] => 2.png
        [1] => 1.png
        [2] => 3.png
        [3] => 4.png
        [4] => 5.png
    )

[submit] => Submit
)

You can notice that the image with the name 2.png (wich I sorted it to be first) is being accomodated in the position [0] of the array as expected. But when I try to upload it I get the variable $is_main set to another image. How can I indentify in the array position [0] to be the main image?

I've tried iterating with $x so that when $x is 0 in the foreach loop I set the first image to be the main one, but looks like in the process the array gets "dissacomodated"

EDIT: If you could at least tell me how to get the first image in the array in PHP I would easily know how to do it with Laravel :-)

1 Answer 1

1

Use reset()

$files = Input::file('images');

foreach($files  as $file):

        $filename = $x.'.'.$file->guessClientExtension();

        # Destination, Name
        if (!file_exists($thepath))
            mkdir($thepath, 0777, true);

        $uploadflag = $file->move($thepath,$filename);

        # reset() files to see if current file is the first one
        $is_main = ($file == reset($files)) ? true : NULL;

        $img_submit = DB::table('images')->insert([
            'image_id' => $filename,
            'is_main'  => $is_main
        ]);

        if($uploadflag) 
            $uploadedfiles[] = $filename;

endforeach;
Sign up to request clarification or add additional context in comments.

19 Comments

Looks like a good solution but It's not working this way unfortunately :(
Why is it not working for you? What is the issue? try $files[0] == $file
I can successfully upload the images and store them to database, but what I want is to set the first image ( in the array would be position[0] ) as the main one so $is_main would be set to TRUE, I tried your code but I still get the same result - the images are stored in the order I uploaded them but not in the order I arranged them with this plugin: bit.ly/1BPGG8v (Example #4)
Yep that was it, I lost the sorting functionality.... Hmm, thanks ALOT for your help, I'll try to figure this out somehow :/
@Daniel your best bet is to implement AJAX the way the plugin was designed.
|

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.