I have this situation: I have a multiple input file but I need to limit the images on database. So I set a limit, and everytime an upload occurs, the limit is checked, but I'm having problems with the loop.
If the limit is 4 images, there are 2 stored on DB, and the user send 5 files, it inserts the 5 files. It must insert 2 and discard the other 3 images.
I need a loop that does that math.
This is my code so far:
$prod_id = $_POST['prod_id'];
$img_limit = 4;
# ----------------------------------------
$sql = "SELECT * FROM images WHERE prod_id = ?";
$arrayParam = array($prod_id);
$data_img = $crud->getSQLGeneric($sql, $arrayParam, true);
$total_img = count($data_img);
# ----------------------------------------
if($total_img == $img_limit)
{
echo '<script type="text/javascript">';
echo 'alert("You already have '.$img_limit.' images inserted");';
echo 'window.history.back();';
echo '</script>';
exit;
}
# ----------------------------------------
$max_files = $img_limit - $total_img;
$fileCount = count($_FILES["img_file"]['name']);
# ----------------------------------------
for($i=0; $i < $fileCount; $i++)
{
$allowed_ext = array('bmp','jpg','jpeg','png','svg');
$array_ext = explode('.', $_FILES['img_file']['name'][$i]);
$file_ext = strtolower(end($array_ext));
# -----
$img_path = 'upload/';
$img_tmp = $_FILES['img_file']['tmp_name'][$i];
$img_name = md5(date('d_m_Y_H_i_s').'_'.$_FILES['img_file']['name'][$i]).'.'.$file_ext;
# -----
$arrayInsert = array('prod_id' => $prod_id, 'img_name' => $img_name);
$return = $crud->insert($arrayInsert);
# -----
move_uploaded_file($img_tmp, $img_path.$img_name);
}
echo "Image(s) uploaded!";
Any help is welcome! =) Thanks