如何限制用户在一个月内使用php/mysql只上传三张图片

kmpatx3s  于 2021-06-18  发布在  Mysql
关注(0)|答案(3)|浏览(274)

我正在研究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;

我想一个用户只能上传3个月的图像。
请帮助我的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>

请帮我分享最好的解决方案——一个用户一个月可以上传三张图片。

5cg8jx4n

5cg8jx4n1#

基本思想是统计给定用户在给定月份上传的所有图像,如:

SELECT COUNT(*) FROM `images` WHERE `user_name` = ? GROUP BY MONTH(`date`);

如果上面的查询返回 3 ,您可以阻止上载。

hfsqlsce

hfsqlsce2#

考虑以下几点。。。

CREATE TABLE my_table
(id SERIAL PRIMARY KEY
,user_id INT NOT NULL
,date DATE NOT NULL
);

INSERT INTO my_table (user_id,date)
SELECT 1
     , '2018-01-01'
  FROM (SELECT 1) x
  LEFT
  JOIN (SELECT user_id
             , DATE_FORMAT(date,'%Y-%m') ym
          FROM my_table
         GROUP
            BY user_id
             , ym
        HAVING COUNT(*) >=3
       ) y
    ON y.user_id = 1
   AND y.ym = DATE_FORMAT('2018-01-01','%Y-%m')
 WHERE y.user_id IS NULL;
 LIMIT 1;

SELECT * FROM my_table;
+----+---------+------------+
| id | user_id | date       |
+----+---------+------------+
|  1 |       1 | 2018-01-01 |
+----+---------+------------+

INSERT INTO my_table (user_id,date)
SELECT 1
     , '2018-01-02'
  FROM (SELECT 1) x
  LEFT
  JOIN (SELECT user_id
             , DATE_FORMAT(date,'%Y-%m') ym
          FROM my_table
         GROUP
            BY user_id
             , ym
        HAVING COUNT(*) >=3
       ) y
    ON y.user_id = 1
   AND y.ym = DATE_FORMAT('2018-01-02','%Y-%m')
 WHERE y.user_id IS NULL
 LIMIT 1;

SELECT * FROM my_table;
+----+---------+------------+
| id | user_id | date       |
+----+---------+------------+
|  1 |       1 | 2018-01-01 |
|  2 |       1 | 2018-01-02 |
+----+---------+------------+

INSERT INTO my_table (user_id,date)
SELECT 1
     , '2018-01-02'
  FROM (SELECT 1) x
  LEFT
  JOIN (SELECT user_id
             , DATE_FORMAT(date,'%Y-%m') ym
          FROM my_table
         GROUP
            BY user_id
             , ym
        HAVING COUNT(*) >=3
       ) y
    ON y.user_id = 1
   AND y.ym = DATE_FORMAT('2018-01-02','%Y-%m')
 WHERE y.user_id IS NULL
 LIMIT 1;
Query OK, 1 row affected (0.00 sec)

SELECT * FROM my_table;
+----+---------+------------+
| id | user_id | date       |
+----+---------+------------+
|  1 |       1 | 2018-01-01 |
|  2 |       1 | 2018-01-02 |
|  3 |       1 | 2018-01-02 |
+----+---------+------------+

INSERT INTO my_table (user_id,date)
SELECT 1
     , '2018-01-03'
  FROM (SELECT 1) x
  LEFT
  JOIN (SELECT user_id
             , DATE_FORMAT(date,'%Y-%m') ym
          FROM my_table
         GROUP
            BY user_id
             , ym
        HAVING COUNT(*) >=3
       ) y
    ON y.user_id = 1
   AND y.ym = DATE_FORMAT('2018-01-03','%Y-%m')
 WHERE y.user_id IS NULL
 LIMIT 1;
Query OK, 0 rows affected (0.00 sec)

SELECT * FROM my_table;
+----+---------+------------+
| id | user_id | date       |
+----+---------+------------+
|  1 |       1 | 2018-01-01 |
|  2 |       1 | 2018-01-02 |
|  3 |       1 | 2018-01-02 |
+----+---------+------------+

INSERT INTO my_table (user_id,date)
SELECT 1
     , '2018-02-03'
  FROM (SELECT 1) x
  LEFT
  JOIN (SELECT user_id
             , DATE_FORMAT(date,'%Y-%m') ym
          FROM my_table
         GROUP
            BY user_id
             , ym
        HAVING COUNT(*) >=3
       ) y
    ON y.user_id = 1
   AND y.ym = DATE_FORMAT('2018-02-03','%Y-%m')
 WHERE y.user_id IS NULL
 LIMIT 1;
Query OK, 1 row affected (0.00 sec)

SELECT * FROM my_table;
+----+---------+------------+
| id | user_id | date       |
+----+---------+------------+
|  1 |       1 | 2018-01-01 |
|  2 |       1 | 2018-01-02 |
|  3 |       1 | 2018-01-02 |
|  4 |       1 | 2018-02-03 |
+----+---------+------------+
x4shl7ld

x4shl7ld3#

@fabrik解决方案是最简单的方法,但在处理大量图像/用户时,它的性能会急剧下降。
如果你不介意对解决方案再深入一点,我建议加上 image_upload_credit 字段到用户表。每次用户上传的图像,你会减少1学分。
因为你已经可以访问 User 对象时,插入的复杂性是 O(1) .
要完成解决方案,您需要编写一个 cron (定期运行)重置 image_upload_credit 每月的第一天 UPDATE user SET image_upload_credit = 3 WHERE image_upload_credit <> 3;

相关问题