如何限制用户使用PHP / MySQL在一个月内仅上传三个图像
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何限制用户使用PHP / MySQL在一个月内仅上传三个图像,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3516字,纯文字阅读大概需要6分钟。
内容图文
![如何限制用户使用PHP / MySQL在一个月内仅上传三个图像](/upload/InfoBanner/zyjiaocheng/887/e5b772d522b346abbf50e2c1b736793f.jpg)
我正在使用PHP.我的问题是如何限制用户一个月只能上传三张图片.
Mysql数据库表-
CREATE TABLE `images` (
`id` int(40) NOT NULL,
`user_name` varchar(40) NOT NULL,
`mobile` varchar(30) NOT NULL,
`email` varchar(50) NOT NULL,
`name` longblob NOT NULL,
`position` int(40) NOT NULL,
`date` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
我希望用户在一个月内只能上传三张图片.
请帮助我使用PHP脚本.我是PHP的初学者.分享最佳解决方案..
我用这个-
<?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>';
// }
}
}
?>
还有我的HTML表单-
<form method="post" id="frm-image-upload" action="my-account.php#parentHorizontalTab3" name='img'
method="post" enctype="multipart/form-data">
<div class="agileits_w3layouts_contact_left"style="margin-left:20%;">
<input type="hidden" name="user_name" value="<?php $space = " ";
echo $row["fname"].$space.$row["lname"]; ?>" id="user_name" Placeholder="Your Name" required />
<input type="hidden" name="mobile" value="<?php echo $row["mobile"]; ?>" id="mobile"Placeholder="Mobile" required />
<input type="hidden" name="email" value="<?php echo $row["email"]; ?>" id="email" Placeholder="Email" required/>
</div>
<center>
<input type="file" name="image" id="image" />
</center>
<br />
<center><input type="submit" name="insert" id="insert" value="Upload" class="btn btn-info" /> </center>
</form>
请帮助我分享最佳解决方案-用户一个月内可以上传三张图片.
解决方法:
基本思想是计算给定用户在给定月份上传的所有图像,例如:
SELECT COUNT(*) FROM `images` WHERE `user_name` = ? GROUP BY MONTH(`date`);
如果上面的查询返回3,则可以阻止上载.
内容总结
以上是互联网集市为您收集整理的如何限制用户使用PHP / MySQL在一个月内仅上传三个图像全部内容,希望文章能够帮你解决如何限制用户使用PHP / MySQL在一个月内仅上传三个图像所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。