我有这样的情况:我有一个多输入文件,但我需要限制数据库中的图像。所以我设置了一个限制,每次上传的时候,限制都会被检查,但是我的循环有问题。
如果限制为4个图像,则数据库中存储了2个图像,并且用户发送了5个文件,则会插入这5个文件。它必须插入2个图像并丢弃其他3个图像。
我需要一个循环来计算。
这是我目前的代码:
$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!";
欢迎任何帮助!=)
谢谢