Limit of uploading documents

2020-04-01 00:36发布

How to limit uploading documents!

For example:- If the database already has 5 entries, it should not take the 6th entry. And show You can only have 5 documents

My Code:-

<?php

    error_reporting( ~E_NOTICE ); // avoid notice

    require_once 'dbconfig.php';

    if(isset($_POST['btnsave']))
    {
        $username = $_POST['user_name'];// user name
        $userjob = $_POST['user_job'];// user email

        $imgFile = $_FILES['user_image']['name'];
        $tmp_dir = $_FILES['user_image']['tmp_name'];
        $imgSize = $_FILES['user_image']['size'];


        if(empty($username)){
            $errMSG = "Please Enter Name.";
        }
        else if(empty($userjob)){
            $errMSG = "Please Enter Description.";
        }
        else if(empty($imgFile)){
            $errMSG = "Please Select Image File.";
        }
        else
        {
            $upload_dir = 'user_images/'; // upload directory

            $imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension

            // valid image extensions
            $valid_extensions = array('jpeg', 'jpg', 'png', 'gif', 'txt'); // valid extensions

            // rename uploading image
            $userpic = rand(1000,1000000).".".$imgExt;

            // allow valid image file formats
            if(in_array($imgExt, $valid_extensions)){           
                // Check file size
                if($imgSize < 10000000)             {
                    move_uploaded_file($tmp_dir,$upload_dir.$userpic);
                }
                else{
                    $errMSG = "Sorry, your file is too large.";
                }
            }
            else{
                $errMSG = "Sorry, this file is not allowed.";       
            }
        }


        // if no error occured, continue ....
        if(!isset($errMSG))
        {
            $stmt = $DB_con->prepare('INSERT INTO tbl_users(userName,userProfession,userPic) VALUES(:uname, :ujob, :upic)');
            $stmt->bindParam(':uname',$username);
            $stmt->bindParam(':ujob',$userjob);
            $stmt->bindParam(':upic',$userpic);

            if($stmt->execute())
            {
                $successMSG = "new record succesfully inserted ...";
                header("refresh:1;index.php"); // redirects image view page after 1 seconds.
            }
            else
            {
                $errMSG = "error while inserting....";
            }
        }
    }
?>

So, what should I add that give my output!

I want only 5 documents in my database. If user tries to add more than 5 documents, the error should be shown.

标签: php mysql
3条回答
2楼-- · 2020-04-01 00:48

First count tbl_users data and check if rows smaller than 5 , insert new data:

$errMSG = "";
error_reporting( ~E_NOTICE ); // avoid notice
require_once 'dbconfig.php';
$continue = true;
$data = $DB_con->query("SELECT COUNT(*) AS rows FROM tbl_users WHERE 1")->fetchall();
$count = $data[0]['rows'];
if($count >= 5)
    $continue = false;
if($continue):
    if(isset($_POST['btnsave']))
    {
        $username = $_POST['user_name'];// user name
        $userjob = $_POST['user_job'];// user email

        $imgFile = $_FILES['user_image']['name'];
        $tmp_dir = $_FILES['user_image']['tmp_name'];
        $imgSize = $_FILES['user_image']['size'];


        if(empty($username)){
            $errMSG = "Please Enter Name.";
        }
        else if(empty($userjob)){
            $errMSG = "Please Enter Description.";
        }
        else if(empty($imgFile)){
            $errMSG = "Please Select Image File.";
        }
        else
        {
            $upload_dir = 'user_images/'; // upload directory

            $imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension

            // valid image extensions
            $valid_extensions = array('jpeg', 'jpg', 'png', 'gif', 'txt'); // valid extensions

            // rename uploading image
            $userpic = rand(1000,1000000).".".$imgExt;

            // allow valid image file formats
            if(in_array($imgExt, $valid_extensions)){           
                // Check file size
                if($imgSize < 10000000)             {
                    move_uploaded_file($tmp_dir,$upload_dir.$userpic);
                }
                else{
                    $errMSG = "Sorry, your file is too large.";
                }
            }
            else{
                $errMSG = "Sorry, this file is not allowed.";       
            }
        }


        // if no error occured, continue ....
        if(!isset($errMSG))
        {
            $stmt = $DB_con->prepare('INSERT INTO tbl_users(userName,userProfession,userPic) VALUES(:uname, :ujob, :upic)');
            $stmt->bindParam(':uname',$username);
            $stmt->bindParam(':ujob',$userjob);
            $stmt->bindParam(':upic',$userpic);

            if($stmt->execute())
            {
                $successMSG = "new record succesfully inserted ...";
                header("refresh:1;index.php"); // redirects image view page after 1 seconds.
            }
            else
            {
                $errMSG = "error while inserting....";
            }
        }
    }
else:
    $errMSG = "You already insert 5 rows";
endif;
查看更多
够拽才男人
3楼-- · 2020-04-01 00:52

Consider the following:

DROP TABLE my_table;

CREATE TABLE my_table
 (id int auto_increment PRIMARY KEY
 ,val char(1) NOT NULL
 );

Query OK, 0 rows affected (0.02 sec)

INSERT INTO my_table (val) SELECT 'b' FROM (SELECT 1) x WHERE (SELECT COUNT(*) FROM my_table) < 5;
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

INSERT INTO my_table (val) SELECT 'b' FROM (SELECT 1) x WHERE (SELECT COUNT(*) FROM my_table) < 5;
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

INSERT INTO my_table (val) SELECT 'b' FROM (SELECT 1) x WHERE (SELECT COUNT(*) FROM my_table) < 5;
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

INSERT INTO my_table (val) SELECT 'b' FROM (SELECT 1) x WHERE (SELECT COUNT(*) FROM my_table) < 5;
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

INSERT INTO my_table (val) SELECT 'b' FROM (SELECT 1) x WHERE (SELECT COUNT(*) FROM my_table) < 5;
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

INSERT INTO my_table (val) SELECT 'b' FROM (SELECT 1) x WHERE (SELECT COUNT(*) FROM my_table) < 5;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

SELECT * FROM my_table;
+----+-----+
| id | val |
+----+-----+
|  1 | b   |
|  2 | b   |
|  3 | b   |
|  4 | b   |
|  5 | b   |
+----+-----+
查看更多
小情绪 Triste *
4楼-- · 2020-04-01 01:04

+1 vote to aidinMC

Answer of aidinMC partially solves your question.

There are two small mistakes in aidinMC answer

1) Strike of : from else:

  }
else
    $errMSG = "You already insert 5 rows";
endif;

2) Change if($count >= 5) to if($count < 5)

$count = $data[0]['rows'];
if($count < 5)
{

After changing these two errors Answer of aidinMC will work! But after seeing your comments especially Limit of uploading documents & Limit of uploading documents it will not give the result as you want.

So what you want is here:-

<?php
error_reporting( ~E_NOTICE ); // avoid notice
require_once 'dbconfig.php';

    if(isset($_POST['btnsave']))
    {
        $username = $_POST['user_name'];// user name
        $userjob = $_POST['user_job'];// user email

        $imgFile = $_FILES['user_image']['name'];
        $tmp_dir = $_FILES['user_image']['tmp_name'];
        $imgSize = $_FILES['user_image']['size'];


        if(empty($username)){
            $errMSG = "Please Enter Name.";
        }
        else if(empty($userjob)){
            $errMSG = "Please Enter Description.";
        }
        else if(empty($imgFile)){
            $errMSG = "Please Select Image File.";
        }
        else
        {
            $upload_dir = 'user_images/'; // upload directory

            $imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension

            // valid image extensions
            $valid_extensions = array('jpeg', 'jpg', 'png', 'gif', 'txt'); // valid extensions

            // rename uploading image
            $userpic = rand(1000,1000000).".".$imgExt;

            // allow valid image file formats
            if(in_array($imgExt, $valid_extensions)){           
                // Check file size
                if($imgSize < 10000000)             {
                    move_uploaded_file($tmp_dir,$upload_dir.$userpic);
                }
                else{
                    $errMSG = "Sorry, your file is too large.";
                }
            }
            else{
                $errMSG = "Sorry, this file is not allowed.";       
            }
        }


        // if no error occured, continue ....
        if(!isset($errMSG))
        {
            $stmt = $DB_con->prepare('INSERT INTO tbl_users(userName,userProfession,userPic) VALUES(:uname, :ujob, :upic)');
            $stmt->bindParam(':uname',$username);
            $stmt->bindParam(':ujob',$userjob);
            $stmt->bindParam(':upic',$userpic);
$data = $DB_con->query("SELECT COUNT(*) AS rows FROM tbl_users WHERE 1")->fetchall();
$count = $data[0]['rows'];
if($count < 5)
{
            if($stmt->execute())
            {
                $successMSG = "new record succesfully inserted ...";
                header("refresh:1;index.php"); // redirects image view page after 1 seconds.
            }
            else
            {
                $errMSG = "error while inserting....";
            }
        }
        else
{
    $errMSG = "You already insert 5 rows";
}
    }
}

?>

I have just edited placement of codes Answered by aidinMC and fixed some bugs in Answer of aidinMC.

Hope this will work.

查看更多
登录 后发表回答