Php 上传文件的限制
如何限制上传文件 例如:-如果数据库已有5个条目,则不应使用第6个条目。并显示您只能有5个文档 我的代码:-Php 上传文件的限制,php,mysql,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
<?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个以上的文档,则应显示错误。第一次计数
tbl\u用户
数据,并检查行数是否小于5,插入新数据:
$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;
$errMSG=”“;
错误报告(~E_通知);//避而不谈
需要一次'dbconfig.php';
$continue=true;
$data=$DB_con->query(“选择COUNT(*)作为来自tbl_用户的行,其中1”)->fetchall();
$count=$data[0]['rows'];
如果($count>=5)
$continue=false;
如果($继续):
如果(isset($_POST['btnsave']))
{
$username=$\u POST['user\u name'];//用户名
$userjob=$\u POST['user\u job'];//用户电子邮件
$imgFile=$\u文件['user\u image']['name'];
$tmp_dir=$_文件['user_image']['tmp_name'];
$imgSize=$\u文件['user\u image']['size'];
if(空($username)){
$errMSG=“请输入名称。”;
}
else if(空($userjob)){
$errMSG=“请输入说明。”;
}
else if(空($imgFile)){
$errMSG=“请选择图像文件。”;
}
其他的
{
$upload_dir='user_images/';//上载目录
$imgExt=strtolower(路径信息($imgFile,路径信息_扩展));//获取图像扩展
//有效的图像扩展
$valid_extensions=array('jpeg','jpg','png','gif','txt');//有效扩展
//重命名上载图像
$userpic=兰特(10000000)。“..$imgExt;
//允许使用有效的图像文件格式
if(在数组中($imgExt,$valid_扩展名)){
//检查文件大小
如果($imgSize<10000000){
移动上传的文件($tmp\u dir,$upload\u dir.$userpic);
}
否则{
$errMSG=“对不起,您的文件太大。”;
}
}
否则{
$errMSG=“对不起,不允许使用此文件。”;
}
}
//如果没有发生错误,请继续。。。。
如果(!isset($errMSG))
{
$stmt=$DB\u con->prepare('INSERT INTO-tbl\u users(userName,userProfession,userPic)值(:uname,:ujob,:upic)');
$stmt->bindParam(':uname',$username);
$stmt->bindParam(':ujob',$userjob);
$stmt->bindParam(':upic',$userpic);
如果($stmt->execute())
{
$successsg=“新记录成功插入…”;
header(“refresh:1;index.php”);//在1秒后重定向图像视图页面。
}
其他的
{
$errMSG=“插入时出错…”;
}
}
}
其他:
$errMSG=“您已经插入了5行”;
endif;
第一次计数tbl_用户
数据并检查行数是否小于5,插入新数据:
$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;
$errMSG=”“;
错误报告(~E_通知);//避而不谈
需要一次'dbconfig.php';
$continue=true;
$data=$DB_con->query(“选择COUNT(*)作为来自tbl_用户的行,其中1”)->fetchall();
$count=$data[0]['rows'];
如果($count>=5)
$continue=false;
如果($继续):
如果(isset($_POST['btnsave']))
{
$username=$\u POST['user\u name'];//用户名
$userjob=$\u POST['user\u job'];//用户电子邮件
$imgFile=$\u文件['user\u image']['name'];
$tmp_dir=$_文件['user_image']['tmp_name'];
$imgSize=$\u文件['user\u image']['size'];
if(空($username)){
$errMSG=“请输入名称。”;
}
else if(空($userjob)){
$errMSG=“请输入说明。”;
}
else if(空($imgFile)){
$errMSG=“请选择图像文件。”;
}
其他的
{
$upload_dir='user_images/';//上载目录
$imgExt=strtolower(路径信息($imgFile,路径信息_扩展));//获取图像扩展
//有效的图像扩展
$valid_extensions=array('jpeg','jpg','png','gif','txt');//有效扩展
//重命名上载图像
$userpic=兰特(10000000)。“..$imgExt;
//允许使用有效的图像文件格式
if(在数组中($imgExt,$valid_扩展名)){
//检查文件大小
如果($imgSize<10000000){
移动上传的文件($tmp\u dir,$upload\u dir.$userpic);
}
否则{
$errMSG=“对不起,您的文件太大。”;
}
}
否则{
$errMSG=“对不起,不允许使用此文件。”;
}
}
//如果没有发生错误,请继续。。。。
如果(!isset($errMSG))
{
$stmt=$DB\u con->prepare('INSERT INTO-tbl\u users(userName,userProfession,userPic)值(:uname,:ujob,:upic)');
$stmt->bindParam(':uname',$username);
$stmt->bindParam(':ujob',$userjob);
$stmt->bindParam(':upic',$userpic);
如果($stmt->execute())
{
$successsg=“新记录成功插入…”;
header(“refresh:1;index.php”);//在1秒后重定向图像视图页面。
}
其他的
{
$errMSG=“插入时出错…”;
}
}
}
其他:
$errMSG=“您已经插入了5行”;
endif;
+1票赞成
当然部分解决了你的问题
这本书有两个小错误
1) :
从其他:
2) 如果($count>=5)将更改为if($count<5)
$count=$data[0]['rows'];
如果($count<5)
{
成龙之后
$count = $data[0]['rows'];
if($count < 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);
$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";
}
}
}
?>
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 |
+----+-----+