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 :-)