php - 上传文件限制

标签 php mysql

如何限制上传文档!

例如:- 如果数据库已有 5 个条目,则不应采用第 6 个条目。并显示您只能有5个文档

我的代码:-

<?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....";
            }
        }
    }
?>

那么,我应该添加什么来提供我的输出!

我的数据库中只需要 5 个文档。如果用户尝试添加超过 5 个文档,则应显示错误。

最佳答案

+1 票给 aidinMC

AnsweraidinMC部分解决了您的问题。

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)
{

改完这两个错误后AnsweraidinMC将工作!但尤其看到你的评论后Limit of uploading documents & Limit of uploading documents它不会给出你想要的结果。

所以你想要的就在这里:-

<?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";
}
    }
}

?>

我刚刚编辑了代码的放置 Answered通过 aidinMC并修复了 Answer 中的一些错误的aidinMC .

希望这会起作用。

关于php - 上传文件限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46138084/

相关文章:

php - Smarty 与 JQuery 的冲突

java - 从 PHP 与 shell 调用 Java - java.lang.NoClassDefFoundError :

php - 如果数据库不存在则添加行

php - CSS通过包含查询字符串的id选择元素

Java 列出连接到数据库的参数?

mysql - 如何在 mysql 中创建更高的自动增量 ID 号

php - 坚持使用 php 中的字符编码

mysql - 从 MySQL 将 500 万行数据加载到 Pandas

MySQL按请求顺序排序

PHP:反向唯一哈希