array to string error in multiple ulpoad files - php

I Have A form that's have two inputs file one regular and the second is multiple and in one if statment i get them both but in the part
if (!empty($_FILES['logo']['name']) && !empty($_FILES['materials']['name']))
the
$_FILES['materials']
is the multiple
i have tried to
json_encode()
the
$_FILES['materials']
but isn't work, its not pass the first if to the
is_uploaded_file()
/php/
define('IMG_MAX_SIZE', 1024 * 1024 * 5);
if (isset($_POST['submit'])) {
$ex = ['jpg', 'png', 'jpeg', 'gif', 'bmp'];
if (!empty($_FILES['logo']['name']) && !empty($_FILES['materials']['name'])
) {
$matToStr = json_encode($_FILES['materials']);
if (is_uploaded_file($_FILES['logo']['tmp_name']) &&
is_uploaded_file($matToStr)) {
echo "in";
if ($_FILES['logo']['error'] == 0 && $_FILES['logo']['size'] <=
IMG_MAX_SIZE && $_FILES['materials']['error'] == 0 &&
$_FILES['materials']['size'] <= IMG_MAX_SIZE) {
echo "in2";
$logoInfo = pathinfo($_FILES['logo']['name']);
$materialsInfo = pathinfo($_FILES['materials']['name']);
if (in_array(strtolower($logoInfo['extension']), $ex) && in_array(strtolower($materialsInfo['extension']), $ex)) {
$logo = date('Y.m.d.H.i.s') . '-' . $_FILES['logo']['name'];
$materials = date('Y.m.d.H.i.s') . '-' . $_FILES['materials']['name'];
$_SESSION['logo'] = $logo;
$_SESSION['materials'] = $materials;
move_uploaded_file($_FILES['logo']['tmp_name'], 'uploads/' . $logo);
move_uploaded_file($_FILES['materials']['tmp_name'], 'uploads/' . $materials);
$pdo = DB();
$stmt = $pdo->prepare("INSERT INTO client_form_7 (client_id, logo, materials, websites) VALUES (:client_id, :logo, :materials, :websites)");
$stmt->bindParam("client_id", $user_id, PDO::PARAM_INT);
$stmt->bindParam("logo", $logo, PDO::PARAM_STR);
$stmt->bindParam("materials", $materials, PDO::PARAM_STR);
$stmt->bindParam("websites", $websites, PDO::PARAM_STR);
$stmt->execute();
if ($stmt->rowCount() > 0) {
header('location: client-form-7.php?sm=תמונת הפרופיל עודכנה');
exit;
}
}
}
}
}
}
/html/
<form action="" method="post" class="col s12" enctype="multipart/form-data">
<h4>7. קבצים וחומרים</h4>
<div class="row">
<div class="file-field input-field col s6">
<p>לוגו</p>
<div class="btn">
<span>בחר קובץ</span>
<input type="file" name="logo" id="logo">
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text">
</div>
</div>
</div>
<div class="row">
<div class="file-field input-field col s6">
<p>צילומים וחומרים שעלולים להיות רלוונטי</p>
<div class="btn">
<span>בחר קובץ</span>
<input type="file" name="materials[]" id="materials" multiple>
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text">
</div>
</div>
</div>
<div class="row">
<div class="input-field col s6">
<input type="text" name="websites" id="websites" value="<?=old('websites')?>">
<label for="websites">אתרי אינטרנט שניתן למשוך משם חומרים</label>
</div>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<input type="submit" class="btn left" value="העלה קבצים" name="submit">
</div>
</div>
</form>
i expected tha'ts upload the one regular file input
and the scond upload like array because i want to fetch it later

Related

Newbie - If image field empty use current profile picture - PHP

Good day, I am writing a PHP page to update a user's profile details, such as first name, last name, email address mobile number and a profile picture. Everything works fine, I can update a profile picture and store the file name and extension in the database and display it wherever I want to.
BUT
Let's say today I update my profile picture.
Let's say tomorrow I want to change only one detail (MY NAME)
When you update only one detail, I think the file input is empty.
Is there a way to use the value already existing in the database for the profile picture instead of updating an empty field???
<?php echo htmlentities($result->image);?>
PHP CODE:
<?php
session_start();
error_reporting(0);
include('includes/config.php');
if(strlen($_SESSION['alogin'])==0)
{
header('location:login.php');
}
else{
if(isset($_POST['submit']))
{
$file = rand(1000,100000)."-".$_FILES['image']['name'];
$file_loc = $_FILES['image']['tmp_name'];
$file_size = $_FILES['image']['size'];
$file_type = $_FILES['image']['type'];
$folder="assets/images/user-pp/";
/* new file size in KB */
$new_size = $file_size/1024;
/* new file size in KB */
/* make file name in lower case */
$new_file_name = strtolower($file);
/* make file name in lower case */
$final_file=str_replace(' ','-',$new_file_name);
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
$mobile=$_POST['mobile'];
$idedit=$_POST['editid'];
$image=$_POST['image'];
if(move_uploaded_file($file_loc,$folder.$final_file))
{
$image=$final_file;
}
$sql="UPDATE users SET firstname=(:firstname), lastname=(:lastname), email=(:email), mobile=(:mobile), Image=(:image) WHERE id=(:idedit)";
$query = $dbh->prepare($sql);
$query-> bindParam(':firstname', $firstname, PDO::PARAM_STR);
$query-> bindParam(':lastname', $lastname, PDO::PARAM_STR);
$query-> bindParam(':email', $email, PDO::PARAM_STR);
$query-> bindParam(':mobile', $mobile, PDO::PARAM_STR);
$query-> bindParam(':image', $image, PDO::PARAM_STR);
$query-> bindParam(':idedit', $idedit, PDO::PARAM_STR);
$query->execute();
$msg="Information Updated Successfully";
}
?>
HTML FORM
<form method="post" enctype="multipart/form-data">
<div class="row">
<div class="media">
<a href="javascript: void(0);">
<img src="assets/images/user-pp/<?php echo htmlentities($result->image);?>" class="rounded mr-75" alt="profile image" height="64" width="64"></a>
<div class="media-body mt-75">
<div class="col-12 px-0 d-flex flex-sm-row flex-column justify-content-start">
<label class="btn btn-sm btn-primary ml-50 mb-50 mb-sm-0 cursor-pointer" for="account-upload">Upload new photo</label>
<input type="file" name="image" id="account-upload" class="form-control" hidden>
<input type="hidden" class="form-control" value="<?php echo htmlentities($result->image);?>">
<button class="btn btn-sm btn-outline-warning ml-50">Reset</button>
</div>
<p class="text-muted ml-75 mt-50"><small>Allowed JPG, GIF or PNG. Max size of 800kB</small></p>
</div>
</div>
<div class="col-12">
<div class="form-group">
<div class="controls">
<label for="account-username">First Name</label>
<input type="text" name="firstname" class="form-control" id="firstname" placeholder="First Name" value="<?php echo htmlentities($result->firstname);?>" required data-validation-required-message="Your first name is required!">
</div>
</div>
</div>
<div class="col-12">
<div class="form-group">
<div class="controls">
<label for="account-username">Last Name</label>
<input type="text" name="lastname" class="form-control" id="lastname" placeholder="Last Name" value="<?php echo htmlentities($result->lastname);?>" required data-validation-required-message="Your last name is required!">
</div>
</div>
</div>
<div class="col-12">
<div class="form-group">
<div class="controls">
<label for="account-name">Email</label>
<input type="email" name="email" class="form-control" id="email" placeholder="Email" value="<?php echo htmlentities($result->email);?>" required data-validation-required-message="Your email address is required!">
</div>
</div>
</div>
<div class="col-12">
<div class="form-group">
<div class="controls">
<label for="account-e-mail">Mobile</label>
<input type="tel" name="mobile" class="form-control" id="mobile" placeholder="Mobile" value="<?php echo htmlentities($result->mobile);?>" required data-validation-required-message="Your mobile number is required!">
</div>
</div>
</div>
<input type="hidden" name="editid" class="form-control" required value="<?php echo htmlentities($result->id);?>">
<?php
if($error){?>
<h5 class="danger">
<?php echo htmlentities($error); ?>
</h5>
<?php }
else if($msg){?>
<h5 class="success">
<?php echo htmlentities($msg); ?>
</h5>
<?php } ?>
<div class="col-12 d-flex flex-sm-row flex-column justify-content-end">
<button type="submit" name="submit" class="btn btn-primary mr-sm-1 mb-1 mb-sm-0">Save
changes</button>
<button type="reset" class="btn btn-outline-warning">Cancel</button>
</div>
</div>
</form>
Try the code bellow and see if this solves what you are trying to achieve:
<?php
session_start();
error_reporting(0);
include('includes/config.php');
if(strlen($_SESSION['alogin'])==0) {
header('location:login.php');
}
function fileUpload() {
if (!empty($_FILES)) {
$file = rand(1000,100000)."-".$_FILES['image']['name'];
$file_loc = $_FILES['image']['tmp_name'];
$file_size = $_FILES['image']['size'];
$file_type = $_FILES['image']['type'];
$folder = "assets/images/user-pp/";
/* new file size in KB */
$new_size = $file_size/1024;
/* new file size in KB */
/* make file name in lower case */
$new_file_name = strtolower($file);
/* make file name in lower case */
$final_file = str_replace(' ','-',$new_file_name);
try {
//throw exception if can't move the file
if (!move_uploaded_file($file_loc, $folder.$final_file)) {
throw new Exception('Could not move file');
}
return $final_file;
} catch (Exception $e) {
die ('File did not upload: ' . $e->getMessage());
}
}
return "";
}
if(isset($_POST['submit'])) {
$firstname = !empty($_POST['firstname']) ? $_POST['firstname'] : '';
$lastname = !empty($_POST['lastname']) ? $_POST['lastname'] : '';
$email = !empty($_POST['email']) ? $_POST['email'] : '';
$mobile = !empty($_POST['mobile']) ? $_POST['mobile'] : '';
$idedit = !empty($_POST['idedit']) ? $_POST['idedit'] : '';
$image = fileUpload();
try {
$sql = "UPDATE users SET ";
if($firstname != '') { $sql .= "firstname=(:firstname)"; }
if($lastname != '') { $sql .= ", lastname=(:lastname)"; }
if($email != '') { $sql .= ", email=(:email)"; }
if($mobile != '') { $sql .= ", mobile=(:mobile)"; }
if($image != '') { $sql .= ", Image=(:image)"; }
if($idedit != '') { $sql .= " WHERE id=(:idedit)"; }
$query = $dbh->prepare($sql);
if($firstname != '') { $query-> bindParam(':firstname', $firstname, PDO::PARAM_STR); }
if($lastname != '') { $query-> bindParam(':lastname', $lastname, PDO::PARAM_STR); }
if($email != '') { $query-> bindParam(':email', $email, PDO::PARAM_STR); }
if($mobile != '') { $query-> bindParam(':mobile', $mobile, PDO::PARAM_STR); }
if($image != '') { $query-> bindParam(':image', $image, PDO::PARAM_STR); }
if($idedit != '') { $query-> bindParam(':idedit', $idedit, PDO::PARAM_STR); }
$query->execute();
$msg="Information Updated Successfully";
} catch (Exception $e) {
/* if something fails inside try block will be catched here */
print $e->getMessage();
}
}
?>
So the solution was basic, but it took some hard thinking, LOL, for someone who has only been dabbling with code for two months;)
Instead of trying to set a default value for the file input field, rather set the php/mysql at the top of the form page to check
is the file size bigger than 0?
if so, then update all fields including profile picture / avatar / file
(else)
update only specified other fields
HERE IS HOW
php at the top of page
-<?php
session_start();
error_reporting(0);
include('includes/config.php');
if(strlen($_SESSION['alogin'])==0)
{
header('location:login.php');
}
else{
if(isset($_POST[submit]))
{
if($_FILES['image']['size'] > 0)
{
$file = $_FILES['image']['name'];
$file_loc = $_FILES['image']['tmp_name'];
$file_type = $_FILES['image']['type'];
$folder="assets/images/user-pp/";
/* make file name in lower case */
$new_file_name = strtolower($file);
/* make file name in lower case */
$final_file=str_replace(' ','-',$new_file_name);
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
$mobile=$_POST['mobile'];
$idedit=$_POST['editid'];
$image=$_POST['image'];
if(move_uploaded_file($file_loc,$folder.$final_file))
{
$image=$final_file;
}
$sql="UPDATE users SET firstname=(:firstname), lastname=(:lastname), email=(:email), mobile=(:mobile), Image=(:image) WHERE id=(:idedit)";
$query = $dbh->prepare($sql);
$query-> bindParam(':firstname', $firstname, PDO::PARAM_STR);
$query-> bindParam(':lastname', $lastname, PDO::PARAM_STR);
$query-> bindParam(':email', $email, PDO::PARAM_STR);
$query-> bindParam(':mobile', $mobile, PDO::PARAM_STR);
$query-> bindParam(':image', $image, PDO::PARAM_STR);
$query-> bindParam(':idedit', $idedit, PDO::PARAM_STR);
$query->execute();
$msg="Information Updated Successfully";
}
else {
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
$mobile=$_POST['mobile'];
$idedit=$_POST['editid'];
$sql="UPDATE users SET firstname=(:firstname), lastname=(:lastname), email=(:email), mobile=(:mobile) WHERE id=(:idedit)";
$query = $dbh->prepare($sql);
$query-> bindParam(':firstname', $firstname, PDO::PARAM_STR);
$query-> bindParam(':lastname', $lastname, PDO::PARAM_STR);
$query-> bindParam(':email', $email, PDO::PARAM_STR);
$query-> bindParam(':mobile', $mobile, PDO::PARAM_STR);
$query-> bindParam(':idedit', $idedit, PDO::PARAM_STR);
$query->execute();
$msg="Information Updated Successfully";
}
}
?>
<form method="post" enctype="multipart/form-data">
<div class="row">
<div class="media">
<a href="javascript: void(0);">
<img src="assets/images/user-pp/<?php echo htmlentities($result->image);?>" class="rounded mr-75" alt="profile image" height="64" width="64"></a>
<div class="media-body mt-75">
<div class="col-12 px-0 d-flex flex-sm-row flex-column justify-content-start">
<label class="btn btn-sm btn-primary ml-50 mb-50 mb-sm-0 cursor-pointer" for="account-upload">Upload new photo</label>
<input type="file" name="image" id="account-upload" class="form-control" hidden>
<button class="btn btn-sm btn-outline-warning ml-50">Reset</button>
</div>
<p class="text-muted ml-75 mt-50"><small>Allowed JPG, GIF or PNG. Max size of 800kB</small></p>
</div>
</div>
<div class="col-12">
<div class="form-group">
<div class="controls">
<label for="account-username">First Name</label>
<input type="text" name="firstname" class="form-control" id="firstname" placeholder="First Name" value="<?php echo htmlentities($result->firstname);?>" required data-validation-required-message="Your first name is required!">
</div>
</div>
</div>
<div class="col-12">
<div class="form-group">
<div class="controls">
<label for="account-username">Last Name</label>
<input type="text" name="lastname" class="form-control" id="lastname" placeholder="Last Name" value="<?php echo htmlentities($result->lastname);?>" required data-validation-required-message="Your last name is required!">
</div>
</div>
</div>
<div class="col-12">
<div class="form-group">
<div class="controls">
<label for="account-name">Email</label>
<input type="email" name="email" class="form-control" id="email" placeholder="Email" value="<?php echo htmlentities($result->email);?>" required data-validation-required-message="Your email address is required!">
</div>
</div>
</div>
<div class="col-12">
<div class="form-group">
<div class="controls">
<label for="account-e-mail">Mobile</label>
<input type="tel" name="mobile" class="form-control" id="mobile" placeholder="Mobile" value="<?php echo htmlentities($result->mobile);?>" required data-validation-required-message="Your mobile number is required!">
</div>
</div>
</div>
<input type="hidden" name="editid" class="form-control" required value="<?php echo htmlentities($result->id);?>">
<?php
if($error){?>
<div class="col-12">
<div class="alert alert-warning alert-dismissible mb-2" role="alert">
<button type="button" class="close" data-dismiss="danger" aria-label="Close"><span aria-hidden="true">×</span></button>
<p class="mb-0">
<?php echo htmlentities($error); ?>
</p>
</div>
</div>
<?php }
else if($msg){?>
<div class="col-12">
<div class="alert alert-success alert-dismissible mb-2" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span>
</button>
<p class="mb-0">
<?php echo htmlentities($msg); ?>
</p>
</div>
</div>
<?php }
?>
<div class="col-12 d-flex flex-sm-row flex-column justify-content-end">
<button type="submit" name="submit" class="btn btn-primary mr-sm-1 mb-1 mb-sm-0">Save
changes</button>
<button type="reset" class="btn btn-outline-warning">Cancel</button>
</div>
</div>
</form>

move_uploaded_file( ) not working on video/audio/pdf

I've done an upload page that should upload the files and set their name in the database. It works just perfect with the pictures , but the sound formats and the other ones doesn't seem to work.
This is how my html part look
<form method="post" enctype="multipart/form-data">
<div class="card card-login">
<?= FH::csrfInput() ?>
<div class="card-header text-center" data-background-color="rose" >
<h3 class="card-title">Upload</h3>
</div>
<div class="card-content">
<div class="input-group">
<span class="input-group-addon">
</span>
<div class="form-group label-floating">
<label class="control-label"><h4>Chose a name for the file</h4></label>
<br>
<input type="textd" name="name" id="name" class="form-control" value="">
</div>
<br><br>
<div class="form-group label-floating">
<label class="control-label"><h4>Choose a file</h4></label>
<br>
<input type="file" id="file" name="file" >
</div>
</div>
</div>
<div class="footer text-center">
<div class="file-upload">
<label for="submit" class="file-upload__label">
<div class="isa_error_class">
<?= FH::displayErrors($this->displayErrors)?>
</div>
<button class="btn btn-wd btn-lg" data-background-color="rose">Submit</button>
</label>
<input type="submit" name="submit" value="Submit" class="file-upload__input">
</div>
</div>
</form>
And there is the php part
if($this->request->isPost())
{
$this->request->csrfCheck();
$upload->assign($this->request->get());
$upload->user_id = Users::currentUser()->id;
$upload->name .= "." . pathinfo($_FILES['file']['name'] , PATHINFO_EXTENSION);
$value = pathinfo($_FILES['file']['name'] , PATHINFO_EXTENSION);
$upload->format = Upload::setFormat($value);
$dir = Users::currentUser()->id;
if(move_uploaded_file($_FILES["file"]["tmp_name"],'files' . DS . $dir . DS . $upload->name ))
{
if($upload->save())
{
Router::redirect('upload');
}
else
{
$upload->addErrorMessage('file','There were a problem saving in the database.');
}
}
else
{
$upload->addErrorMessage('file','There were a problem uploading it.');
}
}
The DS is the separator. The image formats seems to work perfect , but the other formats don't. Any ideas ?
You should check if u have allowed file_uploads = On in your php.ini and also check the maximum file size upload_max_filesize= 20M and to make sure that you are not passing it.

On submit, not redirecting to action page PHP

When i click on submit, it redirects and show Object Not Found.But the Url shows that it is on the action page.Please help to resolve this issue.....
//addprograms.php - action page
//funtions.php - common page that contain class and functions
HTML CODE
<div class="col-xs-12 col-sm-9 content">
<div class="content-row">
<div class="panel panel-default">
<div class="panel-heading">
<div class="panel-title"><b>Add Programs</b>
</div>
</div>
<div class="panel-body">
<form role="form" class="form-horizontal"
action="../actions/addprograms.php" method="post">
<div class="form-group">
<label class="col-md-2 control-label">Title</label>
<div class="col-md-10">
<input type="text" required="" placeholder="Title"
id="title" class="form-control" name="title">
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label"
for="description">Description</label>
<div class="col-md-10">
<textarea required="" class="form-control"
placeholder="Description" rows="10" cols="30"
name="description"></textarea>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label"
for="exampleInputFile">File input</label>
<div class="col-md-10">
<input type="file" id="exampleInputFile"
name="uploadedfile">
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<button class="btn btn-info" type="submit"
name="addprogramsubmit">Submit</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div><!-- content -->
addprograms.php
<?php
require_once '../includes/functions.php';
if(isset($_POST['addprogramsubmit'])){
echo '12';
exit();
}
?>
This is my functions.php file
functions.php(it is not complete)
<?php
session_start();
class Auth extends DB {
function login($tablename, $username, $password) {
$result = mysqli_query($this->con, "SELECT username,password FROM
$tablename WHERE username='$username'") or die("Error: " .
mysqli_error($this->con));;
$login_result = mysqli_fetch_array($result, MYSQLI_ASSOC);
if (($login_result['username'] == $username) &&
($login_result['password'] == $password)) {
$_SESSION['login_id'] = $username;
}
return true;
}
}
class DB {
public $con;
function __construct() {
$this->con = mysqli_connect("localhost", "root", "", "heatsds");
}
// INSERT
function insert($tablename, $data = array(), $avoid_data = array()) {
$i = 0;
$fields = "";
$values = "";
foreach ($data as $col => $val) {
if (!in_array($col, $avoid_data)) {
if ($i === 0) {
$fields .= "`" . $col . "`";
$values .= "'" . $val . "'";
} else {
$fields .= ",`" . $col . "`";
$values .= ",'" . $val . "'";
}
}
$i++;
}
mysqli_query($this->con, "INSERT INTO $tablename ($fields) VALUES
($values)");
}
function __destruct() {
mysqli_close($this->con);
}
}
Image of the directory
ERROR MESSAGE image
your <form> has action="../actions/addprograms.php", so it will search for addprograms.php file. In your listings it's only actions.php. Move actions.php content into addprograms.php and this should work.

Move uploaded file in php work only sometimes when use multiple uploads

I have a web page for uploading tile image and a concept design image using that tile with a single submit button. But when uploading 2 images with a single submit, move uploaded file is not working always. Sometimes it just moves tile images only, not concept image.
Here is my code:
if(isset($_POST['upload']))
{
$name=$_POST['name'];
$size=$_POST['size'];
$finish=$_POST['finish'];
/* Concept Image */
$concept=$_FILES['concept']['name'];
$contmp=$_FILES['concept']['tmp_name'];
$location='concept';
$upload=move_uploaded_file($contmp,'concept/'.$concept);
$confile='concept/'.$concept;
/* Tile Image */
$image=$_FILES['image']['name'];
$imgtmp=$_FILES['image']['tmp_name'];
$location='tileimage';
$uploading=move_uploaded_file($imgtmp,"tileimage/".$image);
$upfile="tileimage/".$image;
$qry="insert into tile_list value('','$name','$size','$finish','$upfile','$confile')";
$ex=mysqli_query($con,$qry);
$query="insert into availcolors value('','$name','$name','$upfile','$confile')";
$exe=mysqli_query($con,$query);
}
and here is my html markup:
<form method="post" enctype="multipart/form-data" class="form-horizontal">
<div class="form-group">
<label class="control-label col-sm-4">TILE IMAGE<br>
</label>
<div class="col-sm-10 col-md-offset-0 col-md-4">
<input type="file" class="form-control" name=image>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-4">CONCEPT 3D<br>
</label>
<div class="col-sm-10 col-md-offset-0 col-md-4">
<input type="file" class="form-control" name="concept">
</div>
</div>
<div class="col-sm-10 col-md-7 col-md-offset-4">
<button type="submit" name="upload"><img src="images/upload.jpg" alt="" width="106" height="25" class="img-responsive"></button>
</div>
<form>
Please help me to find out what is the problem
Please try this code:
<?php
//error_reporting(0);
if(isset($_POST['upload']))
{
$name = $_POST['name'];
$size = $_POST['size'];
$finish = $_POST['finish'];
/* Concept Image */
$aMyUploads = array();
foreach ($_FILES as $key => $aFile) {
for($i = 0; $i<count($aFile['error']); $i++){
//echo $aFile['error'][$i]; exit;
if(0 === $aFile['error'][$i]){
if($i == 0)
$newLocation = 'concept/'.$aFile['name'][$i];
else if($i == 1)
$newLocation = 'tileimage/'.$aFile['name'][$i];
}
if(0 === $aFile['error'][$i] && (false !== move_uploaded_file($aFile['tmp_name'][$i], $newLocation))){
$aMyUploads[] = $newLocation;
} else {
$aMyUploads[] = '';
}
}
}
if(is_array($aMyUploads)){
$confile=$aMyUploads[0];
$upfile=$aMyUploads[1];
$qry="insert into tile_list value('','$name','$size','$finish','$upfile','$confile')";
$ex=mysqli_query($con,$qry);
$query="insert into availcolors value('','$name','$name','$upfile','$confile')";
$exe=mysqli_query($con,$query);
}else{
echo "ERROR :: Not insert Please try";
}
}
?>
<html>
<form method="post" enctype="multipart/form-data" class="form-horizontal" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div class="form-group">
<label class="control-label col-sm-4">TILE IMAGE<br></label>
<div class="col-sm-10 col-md-offset-0 col-md-4">
<input type="file" class="form-control" name="upload_files[]">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-4">CONCEPT 3D<br></label>
<div class="col-sm-10 col-md-offset-0 col-md-4">
<input type="file" class="form-control" name="upload_files[]">
</div>
</div>
<div class="col-sm-10 col-md-7 col-md-offset-4">
<button type="submit" name="upload"><img src="images/upload.jpg" alt="" width="106" height="25" class="img-responsive"></button>
</div>
</form>
</html>

Cannot able to increment row count in every year

I want to restrict user to upload 50 photos per year. Say for example user can upload 50 photos in 2015 and after that user gets error message. Again in 2016 user can able to upload 50 photos but the photo which user uploads in 2015 will not be deleted.
This is my 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>Home</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 =================================================
}
?>
Change the query to only count the photos in the current year:
SELECT COUNT(*) FROM album WHERE YEAR(date) = YEAR(NOW())

Categories