上载文件数量限制 [英] Limit of uploading documents

查看:71
本文介绍了上载文件数量限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何限制上传文件!

例如:-如果数据库已经有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

答案. com/users/3398171/aidinmc> aidinMC 部分解决了您的问题.

aidinMC 中有两个小错误

2)将if($count >= 5)更改为if($count < 5)

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

更改了这两个错误后,答案 ://stackoverflow.com/users/3398171/aidinmc> aidinMC 可以使用!但是尤其是在看到您的评论后,上传文件的限制& 上传文件的限制,它不会提供您想要的结果. >

所以您想要的是这里:-

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

?>

我刚刚编辑了代码的放置位置aidinMC 并修复了 aidinMC 的答案.

希望这会起作用.

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.

解决方案

+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.

这篇关于上载文件数量限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆