0

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

4
  • 1
    Why do you need a loop for the math? What exactly does not work with the given code? Commented Jun 5, 2018 at 15:24
  • It doesn't look like you're applying or using $max_files at all? Commented Jun 5, 2018 at 15:26
  • you already calculate the max_files. Use it in the for loop instead of filecount and you will insert just the first n images to reach the limit Commented Jun 5, 2018 at 15:26
  • If there are already 2 images on DB, and the user uploads another 5, there will be 7 images. The limit is set on the code. In this case "4". Commented Jun 5, 2018 at 15:27

1 Answer 1

2

Your for loop is not looking for the limit:

# ----------------------------------------
$max_files = $img_limit - $total_img;
$fileCount = count($_FILES["img_file"]['name']);
# ----------------------------------------
for($i=0; $i < $fileCount; $i++)
{

should become

# ----------------------------------------
$max_files = $img_limit - $total_img;
$fileCount = count($_FILES["img_file"]['name']);
# ----------------------------------------
for($i=0; $i < $max_files; $i++)
{

and even if the user will post 5 images (example) it will load only as many images as free slots you have in your db (limit 4 - total 2 then it will upload 2)

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

2 Comments

That's correct. I've tried this and did not work. Now it works. I think I was missing something. Thank you very much!
glad to help. Please mark the solution as correct if it helps you :-)

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.