我想限制用户每年上传50张照片。比如说用户可以在2015年上传50张照片,之后用户会收到错误消息。再次在2016年,用户可以上传50张照片,但用户在2015年上传的照片不会被删除。
这是我的upload.php
<?php
// set timeout period in seconds
$inactive = 100;
// check to see if $_SESSION['timeout'] is set
if(isset($_SESSION['timeout']) ) {
$session_life = time() - $_SESSION['timeout'];
if($session_life > $inactive)
{ session_destroy(); header("Location: logout.php"); }
}
$_SESSION['timeout'] = time();
include "lib/connection.php";
if (!isset($_SESSION['user']))
{
//die ("Access Denied");
header("location:index.php?msg=Please login with valid credentials");
}
if (isset($_SESSION['user']))
{
include "lib/top.php";
include "lib/left.php";
?>
<div class="col-md-10">
<div class="row">
<div class="col-md-12">
<ol class="breadcrumb">
<li><a href="dashboard.php">Home</a></li>
<li class="active">Upload Photo</li>
</ol>
</div>
</div>
<?php
if(!empty($_GET['msg'])) {
$message = $_GET['msg'];
print "$message";
}
?>
<div class="block block-fill-white">
<div class="header">
<h2>Upload Photo</h2>
</div>
<FORM METHOD="POST" ACTION="pic-db-upload.php" enctype = "multipart/form-data">
<div class="content controls">
<div class="form-row">
<div class="col-md-3">Title:</div>
<div class="col-md-9"><input type="text" class="form-control" id="title" name="title" required placeholder="Enter some Title"/></div>
</div>
<div class="form-row">
<div class="col-md-3">Description:</div>
<div class="col-md-9"><textarea class="form-control" id="description" name="description" required></textarea></div>
</div>
<?php
$sel="SELECT * FROM `category`";
$tab=mysql_query($sel);
?>
<div class="form-row">
<div class="col-md-3">Category:</div>
<div class="col-md-9">
<select class="form-control" name="category" required>
<option value="0">Select</option>
<?php
while($row=mysql_fetch_array($tab))
{
?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['cname']; ?></option>
<?php
}
?>
</select>
</div>
</div>
<div class="form-row">
<div class="col-md-3">Thumbnail Height:</div>
<div class="col-md-9"><input type="text" class="form-control" id="theight" name="theight" required placeholder="Enter height"/></div>
</div>
<div class="form-row">
<div class="col-md-3">Thumbnail Width:</div>
<div class="col-md-9"><input type="text" class="form-control" id="twidth" name="twidth" required placeholder="Enter width"/></div>
</div>
<div class="form-row">
<div class="col-md-3">File:</div>
<div class="col-md-9">
<div class="input-group file">
<input type="text" class="form-control"/>
<input type="file" id="picture" name="picture" required/>
<span class="input-group-btn">
<button class="btn btn-primary" type="button">Browse</button>
</span>
</div>
</div>
</div>
<div class="form-row">
<div class="col-md-3"> </div>
<div class="col-md-9" style="align:midle">
<input type="reset" class="btn btn-danger">
<input type="submit" class="btn btn-primary" name="submit" value="UPLOAD">
</div>
</div>
</div>
</div>
</form>
</div>
</div>
<?php
include "lib/bottom.php";
}
?>
and this is my insert query
<?php
ini_set('display_errors', 0);
error_reporting(E_ERROR | E_WARNING | E_PARSE);
ob_start();
include("lib/connection.php");
if (!isset($_SESSION['user']))
{
//die ("Access Denied");
header("location:index.php?msg=Login with valid credentials");
}
if (isset($_SESSION['user']))
{
//======================= INSERT =================================================
if($_POST[submit]=="UPLOAD")
{
if($_POST[title]!="" && $_POST[category]!="" && $_POST[description]!="" && $_FILES[picture][name]=="")
{
header("location:upload-photo.php?msg=You have not inserted any picture ");
}
else
{
$resource = mysql_query("SELECT COUNT(id) FROM album");
$count = mysql_result($resource,0);
if ($count < 50)
{
//======================== THUMBNAIL CODE ========================
function thumb($a,$b)
{
$image_path=$a;
$new_width=$_POST[twidth];
$new_height=$_POST[theight];
$thumb_path=$b;
$ext=strtolower(end(explode('.',$image_path)));
if($ext=='jpg'||$ext=='jpeg'||$ext=='gif'||$ext=='png')
$img=@imagecreatefromjpeg($image_path);
else
die("only jpg or jpeg or gif or png format supported");
$width=imagesx($img);
$height=imagesy($img);
$tmp_img=imagecreatetruecolor($new_width,$new_height);
imagecopyresized($tmp_img,$img,0,0,0,0,$new_width,$new_height,$width,$height);
imagedestroy($img);
imagejpeg($tmp_img,$thumb_path);
}
//==================================================================
$p=md5(rand(1,1000));
$pict=$p.".jpg";
$target_path1 = "big_pic/big_".$pict;
copy($_FILES[picture][tmp_name],$target_path1);
$target_path2="thumb_pic/thumb_".$pict;
thumb($target_path1,$target_path2);
$ins = "INSERT INTO `album`(`id`,`date`,`title`,`description`,`cid`,`picture`,`thumbnail`,`default`)VALUES('',now(),'$_POST[title]','$_POST[description]','$_POST[category]','$target_path1','$target_path2','$pict')" ;
mysql_query($ins);
header("location:upload-photo.php?msg=Photo successfully uploaded");
}
else
{
header("location:upload-photo.php?msg=Sorry maximum number of photo uploaded. Please contact system administrator");
}
}
}
//======================= END INSERT =================================================
}
?>
答案 0 :(得分:0)
将查询更改为仅计算当前年份的照片:
SELECT COUNT(*) FROM album WHERE YEAR(date) = YEAR(NOW())