How to manage user can upload ony three images in PHP .

I have a question How can restrict user can upload only three image in database table .

PHP script

<?php
include(“admin/config.php”);

if(isset($_POST[“insert”]))
{
$mobile = $_POST[‘mobile’];
$email = $_POST[’email’];
$user_name = $_POST[‘user_name’];

$fileinfo = @getimagesize($_FILES[“image”][“tmp_name”]);
$width = $fileinfo[0];
$height = $fileinfo[1];
$allowed_image_extension = array(
“png”,
“jpg”,
“jpeg”
);
$file_extension = pathinfo($_FILES[“image”][“name”], PATHINFO_EXTENSION);

$sql=”select * from images where (name=’$name’);”;

$count=mysqli_query($mysqli,$sql);

$count=count($_FILES[‘name’]);
if($count>3)
{
echo “<font color=’red’>3 image upload </font>”;
} else{

$file_extension = pathinfo($_FILES[“image”][“name”], PATHINFO_EXTENSION);

if (! file_exists($_FILES[“image”][“tmp_name”])) {
$response = array(
“type” => “error”,
“message” => “Choose image file to upload.”
);
}
else if (! in_array($file_extension, $allowed_image_extension)) {
$response = array(
“type” => “error”,
“message” => “<font color=’red’>Upload valiid images. Only PNG and JPEG are allowed.</font>”
);
echo $result;
} // Validate image file size
else if (($_FILES[“image”][“size”] > 2000000)) {
$response = array(
“type” => “error”,
“message” => “Image size exceeds 2MB”
);
} // Validate image file dimension
else if ($width > “1250” || $height > “720”) {
$response = array(
“type” => “error”,
“message” => “<font color=’red’>Image dimension should be within 1250X720</font>”
);

} else {

$target = ‘/image’;
$target = “image/” . basename($_FILES[“image”][“name”]);
$file = addslashes(file_get_contents($_FILES[“image”][“tmp_name”]));

$query = mysqli_query($mysqli,”INSERT INTO images VALUES (”,’$user_name’,’$mobile’,’$email’,’$file’,”,NOW())”);

if (move_uploaded_file($_FILES[“image”][“tmp_name”], $target)) {
$response = array(
“type” => “success”,
“message” => ‘<font color=”green”>Image uploaded successfully </font>’
);

} else {
$response = array(
“type” => “error”,
“message” => “<font color=’red’>Problem in uploading image files.</font>”
);
}
}

// if(mysqli_query($connect, $query))
// {
// echo ‘<script>alert(“Image Inserted into Database”)</script>’;
// }
}

}
?>

I want user can upload only three images and after three images the message will you are unable to upload more image.

Restrict user can upload ony three images . Kindly help me please .

Read more here:: How to manage user can upload ony three images in PHP .

Leave a Reply

Your email address will not be published. Required fields are marked *