PHP多个图像上传检查空图像并上传非空图像

时间:2018-12-16 13:29:04

标签: php mysql arrays mysqli

我需要有关PHP上传具有不同逻辑的多个图像的帮助。
我有1个主图像输入按钮和4个可选图像输入按钮。
第一种逻辑:上传1-4张图片中的所有图片
第二逻辑:请勿上传1-4张图像中的任何图像
第三逻辑:上传一些图片,请勿上传1-4图片中的某些图片。 enter image description here

并检查图像文件的类型和大小不应大于1MB。将图像上传到目标文件夹中,然后在'product_image'表和id as foreign key from product表的'product'表中插入1-4张图像的值,并在 <?php include_once('includes/connect_database.php'); if(isset($_POST['submit'])){ // create array variable to handle error $error = array(); function generateRandomString($length = 8) { $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; $charactersLength = strlen($characters); $randomString = ''; for ($i = 0; $i < $length; $i++) { $randomString .= $characters[rand(0, $charactersLength - 1)]; } return $randomString; } $product_name = $_POST['product_name']; $product_new_name = strtoupper($product_name); $product_price = $_POST['product_price']; $product_image = $_FILES['product_image']['name']; $product_image_tmp = $_FILES['product_image']['tmp_name']; $temp = explode(".", $product_image); $extension = end($temp); $filename = basename($_FILES["product_image"]["name"]); $filename = generateRandomString() . strrchr($filename, '.'); $product_image1 = $_FILES['product_image1']['name']; $product_image_tmp1 = $_FILES['product_image1']['tmp_name']; $temp1 = explode(".", $product_image1); $extension1 = end($temp1); $filename1 = basename($_FILES["product_image1"]["name"]); $filename1 = generateRandomString() . strrchr($filename1, '.'); $product_image2 = $_FILES['product_image2']['name']; $product_image_tmp2 = $_FILES['product_image2']['tmp_name']; $temp2 = explode(".", $product_image2); $extension2 = end($temp2); $filename2 = basename($_FILES["product_image2"]["name"]); $filename2 = generateRandomString() . strrchr($filename2, '.'); $product_image3 = $_FILES['product_image3']['name']; $product_image_tmp3 = $_FILES['product_image3']['tmp_name']; $temp3 = explode(".", $product_image3); $extension3 = end($temp3); $filename3 = basename($_FILES["product_image3"]["name"]); $filename3 = generateRandomString() . strrchr($filename3, '.'); $product_image4 = $_FILES['product_image4']['name']; $product_image_tmp4 = $_FILES['product_image4']['tmp_name']; $temp4 = explode(".", $product_image4); $extension4 = end($temp4); $filename4 = basename($_FILES["product_image4"]["name"]); $filename4 = generateRandomString() . strrchr($filename4, '.'); $allowed_image_extension = array( "png", "jpg", "jpeg" ); // Get image file extension $file_extension = pathinfo($_FILES["product_image"]["name"], PATHINFO_EXTENSION); $file_extension1 = pathinfo($_FILES["product_image1"]["name"], PATHINFO_EXTENSION); $file_extension2 = pathinfo($_FILES["product_image2"]["name"], PATHINFO_EXTENSION); $file_extension3 = pathinfo($_FILES["product_image3"]["name"], PATHINFO_EXTENSION); $file_extension4 = pathinfo($_FILES["product_image4"]["name"], PATHINFO_EXTENSION); if (empty($product_image1) && empty($product_image2) && empty($product_image3) && empty($product_image4)) { // Validate file input to check if is with valid extension if (!in_array($file_extension, $allowed_image_extension)) { $error['error'] = '<span class="label red-alert"><font size="3">Upload valid images. Only PNG and JPG/JPEG are allowed. IF BLOCK</font></span>'; } // Validate image file size else if (($_FILES["product_image"]["size"] > 1000000)) { $error['error'] = '<span class="label red-alert"><font size="3">Image size exceeds 1MB IF BLOCK</font></span>'; } else{ move_uploaded_file($product_image_tmp, "uploads/product/".$filename); $insert = "INSERT INTO product (name, image, price) VALUES ('$product_name','$filename','$product_price')"; $run = mysqli_query($connect, $insert); if ($run) { $error['success'] = '<span class="label green-alert"><font size="3">Successfully Saved. IF BLOCK</font></span>'; }else{ $error['error'] = '<span class="label red-alert"><font size="3">Something Went Wrong. IF BLOCK</font></span>'; } } }else{ // Validate file input to check if is with valid extension if (!in_array($file_extension, $allowed_image_extension) || !in_array($file_extension1, $allowed_image_extension) || !in_array($file_extension2, $allowed_image_extension) || !in_array($file_extension3, $allowed_image_extension) || !in_array($file_extension4, $allowed_image_extension)) { $error['error'] = '<span class="label red-alert"><font size="3">Upload valid images. Only PNG and JPG/JPEG are allowed. ELSE BLOCK</font></span>'; } // Validate image file size else if (($_FILES["product_image"]["size"] > 1000000) || ($_FILES["product_image1"]["size"] > 1000000) || ($_FILES["product_image2"]["size"] > 1000000) || ($_FILES["product_image3"]["size"] > 1000000) || ($_FILES["product_image4"]["size"] > 1000000)) { $error['error'] = '<span class="label red-alert"><font size="3">Image size exceeds 1MB ELSE BLOCK</font></span>'; } else{ move_uploaded_file($product_image_tmp, "uploads/product/".$filename); move_uploaded_file($product_image_tmp1, "uploads/product/".$filename1); move_uploaded_file($product_image_tmp2, "uploads/product/".$filename2); move_uploaded_file($product_image_tmp3, "uploads/product/".$filename3); move_uploaded_file($product_image_tmp4, "uploads/product/".$filename4); $insert = "INSERT INTO product (name, image, price) VALUES ('$product_name','$filename','$product_price')"; $run = mysqli_query($connect, $insert); if ($run) { $error['success'] = '<span class="label green-alert"><font size="3">Successfully Saved. ELSE BLOCK</font></span>'; }else{ $error['error'] = '<span class="label red-alert"><font size="3">Something Went Wrong. ELSE BLOCK</font></span>'; } } } } ?> 表中插入主图像的值。只需查看两个表中的ID 78。

Product Table Product_Image Table

下面是我尝试过的代码:(

<a target="popup" onclick="window.open('', 'popup', 'width=500,height=200,scrollbars=no,toolbar=no,status=no,resizeable=no,menubar=no,location=no,directories=no,top=500,left=500')" href="http://www.your-site.com">Link</a>

0 个答案:

没有答案