How to add multiple images using browse button - php

I have a following code where I can upload a single image. This image gets stored in both database and folder. Now what I want is to add multiple images. How can I do that. Help me to come out of this.
<?php
$uploadDir ="C:/wamp/www/dragongym/customers/";
if(isset($_POST['submit']))
{
$intime =DATE("H:i", STRTOTIME($_POST['intime']));
$outtime =DATE("H:i", STRTOTIME($_POST['outtime']));
date_default_timezone_set('Asia/Calcutta');
$today = date("Y-m-d");
$msg="";
$res = "SELECT customer_id FROM customer ORDER by customer_id DESC LIMIT 1";
$qur = mysql_query($res);
while($row = mysql_fetch_array($qur, MYSQL_BOTH))
{
$last_id = $row['customer_id'];
$plus_id = 1;
}
if( $last_id !="")
{
$cust_id = $last_id + $plus_id;
}
$filePath="";
if($_FILES['cimage']['size'] > 0)
{
// echo $cust_id;
// Temporary file name stored on the server for pdf
$filename = basename($_FILES['cimage']['name']);
$extension = pathinfo($filename, PATHINFO_EXTENSION);
$new = $cust_id.'.'.$extension;
$tmpName1 = $_FILES['cimage']['tmp_name'];
$fileSize = $_FILES['cimage']['size'];
$fileType = $_FILES['cimage']['type'];
$filePath = $uploadDir . $new;
$resultes = move_uploaded_file($tmpName1, $filePath);
if (!$resultes)
{
echo "Error uploading file";
exit;
}
if(!get_magic_quotes_gpc())
{
$new = addslashes($new);
$filePath = addslashes($filePath);
}
}
$sql = 'INSERT INTO customer(customer_name,roll_no,customer_number,customer_address,tariff_id,intime,outtime,customer_image,active,joining_date) VALUES("'.$_POST['name'].'","'.$_POST['roll'].'","'.$_POST['number'].'","'.$_POST['address'].'","'.$_POST['tariff'].'","'.$intime.'","'.$outtime.'","'.$filePath.'","1","'.$today.'")';
$msg="<p style=\"color:#99CC00; font-size:13px;\"> Successfully!</p>";
if (!mysql_query($sql, $link))
{
die('Error: ' . mysql_error());
}
}
?>
<form action="#" method="post" enctype="multipart/form-data">
<h2>Registration Form</h2><?php echo $msg; ?>
<label>Name</label>
<input type="text" value="" name="name" id="name" required class="txtfield">
<label>Roll Number</label>
<input type="text" value="" name="roll" id="roll" required class="txtfield">
<label>Mobile Number</label>
<input type="text" value="" name="number" required class="txtfield" id="mobnum">
<label>Address</label>
<textarea name="address" class="txtfield"></textarea>
<label>Upload Photo</label>
<input type="file" value="" name="cimage" class="txtfield">
<!-- <label style="display: block">Timing</label>
<input type="text" value="" name="intime" placeholder="Intime" required class="timefield timepicker">
<input type="text" value="" name="outtime" placeholder="Outtime" required class="timefield timepicker">-->
<input type="submit" value="Save" name="submit" class="btn buttonside1">
</form>

You can use jquery plugin for that..
there is lots of plugin available on google..try this one http://blueimp.github.io/jQuery-File-Upload/

to do multiple file upload you should first have multiple="true" in your tab like so
<input type="file" name='files[]' multiple='true'/>
then use foreach loop to upload files.

Related

How to remove image from folder at update phpMysql?

Update query from the database:
<?php
include 'dpconnection.php';
$uid=$_REQUEST['upid'];
if (isset($_POST['update'])) {
$category= $_POST['category'];
$pname= $_POST['pname'];
$parea= $_POST['parea'];
$pPrices= $_POST['pPrices'];
$pAddress= $_POST['pAddress'];
$filename = $_FILES['new_image']['name'] ;
$tempname = $_FILES['new_image']['tmp_name'] ;
$filesize = $_FILES['new_image']['size'] ;
$fileextension = explode('.', $filename) ;
$fileextension = strtolower(end($fileextension));
$newfilename = uniqid().'images'.'.'.$fileextension ;
$path = "media/".$newfilename ;
$sql = mysqli_query($conn,"UPDATE product SET c_id='$category', p_name='$pname', p_area='$parea', p_prices='$pPrices',p_address='$pAddress', p_thumb_image='$path' WHERE p_id='$uid'");
if (move_uploaded_file($tempname, $path) && $sql) {
echo "<script>alert('Record Updated successfully');</script>";
echo "<script>location.href = 'adminViewProduct.php';</script>";
} else {
echo "Error updating record: " . $conn->error;
}
}
?>
Image not deleting from folder, i have two if statement but just one else statement so it looks like i am missing something but i really don't know what i am missing.
This is form:
<div class="form-group">
<label>Address</label>
<input class="form-control" type="text" name="pAddress" value="<?php echo $row1['p_address'];?>">
</div>
<div class="form-group">
<label>Thumb Image</label>
<input type="file" name="new_image" class="form-control">
<input class="form-control" type="hidden" name="pimage" value="<?php echo $row1['p_thumb_image'];?>">
<img class="float-left" src="<?php echo $row1['p_thumb_image'];?>" width="100px;"><br><br>
</div>
<div class="form-group text-center">
<input class="btn btn-primary" type="submit" name="update" value="Update">
</div>
</form>
You don't have a code that delete the files in php there's a function called unlink where you can delete a files but first you need fetch the path or filename and delete it before updating your data.
Here is the link of unlink.
https://www.php.net/manual/en/function.unlink.php

Upload in files in for loop

I am using this upload script http://www.dropzonejs.com
The upload php part is:
foreach($_POST["id"] as $key=>$value)
{
$id = trim(mysqli_real_escape_string($mysqli, $value));
$pre_set = trim(mysqli_real_escape_string($mysqli, $_POST['pre_set'][$key]));
$keep_filename = trim(mysqli_real_escape_string($mysqli, $_POST['keep_filename'][$key]));
$ds = DIRECTORY_SEPARATOR;
$storeFolder = '../uploads';
if (!empty($_FILES))
{
$tempFile = $_FILES['file']['tmp_name'];
$targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds;
if($keep_filename == 'yes')
{
$targetFile = $targetPath. $pre_set.'_'.$id.'_'.$_FILES['file']['name'];
}
else
{
$targetFile = $targetPath. $pre_set.'_'.$id.'.'.pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
}
move_uploaded_file($tempFile,$targetFile);
}
}
When using this with an standard form as shown below the file is correctly uploaded and renamed:
<form action="includes/upload.php" class="dropzone">
<input type="hidden" class="form-control" id="id[]" name="id[]" value="'.$row['id'].'">
<input type="hidden" class="form-control" id="pre_set[]" name="pre_set[]" value="logo">
<input type="hidden" class="form-control" id="keep_filename[]" name="keep_filename[]" value="no">
</form>
But when using a for loop situation an image is shown in the upload window but the file is not uploaded.
I am not getting any errors (or not using the correct parameter to see it)
while($row = mysqli_fetch_array($res))
{ $i++;
echo'
<div class="container">
<form action="includes/upload.php">
<div class="form-group col-md-3">';
if($i == 1) { echo '<label>Voeg foto toe</label>'; } echo'
<div class="dropzone dropzone_small" id="myId'.$i.'">
<div class="fallback">
<input type="hidden" class="form-control" id="id[]" name="id[]" value="xxxx">
<input type="hidden" class="form-control" id="pre_set[]" name="pre_set[]" value="toolbox_">
<input type="hidden" class="form-control" id="keep_filename[]" name="keep_filename[]" value="yes">
</div>
</div>
</div>
</form>';
}
Any suggestions to change the code to get this working?
Help is much appreciated.
You must start your form with
<form action="includes/upload.php"> method="post" enctype="multipart/form-data">
// your form attributes here
</form>

select value not getting passed

can some one please tell me why its not getting passsed. all the other value gets passed except for the select option value. the files gets updates perfectly as i want. but the album ID doesnt get assigned as i want :( .
HTML Code from the page:
<form action="" method="post" enctype="multipart/form-data" >
<label>Select Image:</label>
<input type="file" aria-required="true" size="30" id="file" name="files[]" multiple="multiple" />
<?php if(isset($_GET['al'])){ ?>
<label>Album(required):</label>
<select name='album' id='album' aria-required="true">
<?php
$query_album = "SELECT * FROM `albums` ORDER BY `id` DESC ";
$result_m = $conn->query($query_album);
if ($result_m->num_rows > 0) {
// output data of each row
while($item_a = $result_m->fetch_assoc()) {
$value= $item_a['id'];?>
<option value='<?php echo $value; ?>'>
<?php echo $item_a['title']; ?>
</option>
<?php
}
} ?>
</select>
<?php } ?>
<label>Image Title :</label>
<input type="text" aria-required="true" size="30" value="" name='title' id='title'>
<label> Image Description :</label>
<input type="text" aria-required="true" size="30" value="" name='desc' id='desc'>
<button type="submit" value="Upload!" class="soc-icon soc-icon-small soc-icon-alt fa fa-floppy-o">
</button>
<button type="reset" name="reset" class="soc-icon soc-icon-small soc-icon-alt fa fa-refresh">
</button>
<input type="hidden" name="album" id="album" value="<?php echo $album; ?>">
</form>
PHP Code from the UPLOAD PAGE where the values are passed to... all other values gets passed except for the album id. can some one please help me to figure out what i'm doin wrong.
<?php
include 'DBConfig.php';
require 'imageconfig.php';
require 'img_functions.php';
if(isset($_FILES['files'])){
if(!empty($_POST['album'])){
$album_id = $_POST['album'];
$image_title=$_POST['title'];
$image_desc=$_POST['desc'];
$errors= array();
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
$file_name = $key.$_FILES['files']['name'][$key];
$file_size =$_FILES['files']['size'][$key];
$file_tmp =$_FILES['files']['tmp_name'][$key];
$file_type=$_FILES['files']['type'][$key];
if($file_size > 2097152){
$errors[]='File size must be less than 2 MB';
}
$query="INSERT into images(`ID`,`ALBUM_ID`,`CAPTION`,`FILE_NAME`,`FILE_SIZE`,`FILE_TYPE`,`DESC`) VALUES(DEFAULT,'{$album_id}','$image_title','$file_name','$file_size','$file_type','$image_desc'); ";
$desired_dir="uploads/fullsized/";
if(empty($errors)==true){
if(is_dir($desired_dir)==false){
mkdir("$desired_dir", 0700); // Create directory if it does not exist
}
if(is_dir("$desired_dir/".$file_name)==false){
move_uploaded_file($file_tmp,"uploads/fullsized/".$file_name);
create_square_image("uploads/fullsized/".$file_name,"uploads/thumbs/".$file_name,200);
}
else{ //rename the file if another one exist
$new_dir="uploads/fullsized/".$file_name.time();
rename($file_tmp,$new_dir) ;
}
$result = mysqli_query($conn, $query);
}else{
print_r($errors);
}
}
if(empty($error)){
header('Location:gallery.php');
}
}
else {
echo"album id not set";
}
} ?>
You are overwriting your input with another one with the same name:
<select name='album' id='album' aria-required="true">
...
<input type="hidden" name="album" id="album" value="<?php echo $album; ?>">

uploading a image but error of undefined index [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 7 years ago.
I have a form in which i have to upload the image by its category and move the selected category image to specific folder but when i submit the form the following error occurs
( ! ) Notice: Undefined index: pic in C:\wamp\www\movies poster admin\insertform.php on line 26
the pic is the name of the file but don't know why it did not get the value
I know this is a very basic question but i don't know why this error is coming
$imageprice=$_POST['imageprice'];
$imagedescription=$_POST['imagedescription'];
$type=$_POST['category'];
if ($type="celeb") {
$uploadDir = 'C:/wamp/www/Movies Poster Site/celeb/'; //Image Upload Folder
$fileName = $_FILES['pic']['name'];
$tmpName = $_FILES['pic']['tmp_name'];
$fileSize = $_FILES['pic']['size'];
$fileType = $_FILES['pic']['type'];
$filePath = $uploadDir . $fileName;
$filename = $fileName;
$ext = pathinfo($filename, PATHINFO_EXTENSION);
//$date = date_create();
//$fileName= date_timestamp_get($date).".".$ext;
$filePath = $uploadDir . $fileName;
if (isset($fileName)) {
# code...
$result = move_uploaded_file($tmpName, $filePath);
}
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
`
`$sql="INSERT INTO celeb ".
"(image_name,price,description,Type) ".
"VALUES ".
"('$filePath','$imageprice','$imagedescription','$type') ";
mysql_select_db('poster');
$retval = mysql_query( $sql, $connection );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
else
{
echo" <div class='end'>";
echo "Data Entered";
echo"</div>";
mysql_close($connection);
}
Here is the form
<form method="post">
<div class="insertform">
<!-- <input type="text" placeholder="image.jpg" name="imagename" id="imagename"><br>
-->
Select Image Category:<br>
<input type="radio" name="category" value="comic">Comic Posters<br>
<input type="radio" name="category" value="celeb">Celeb Posters<br>
<input type="radio" name="category" value="islamic">Islamic Posters<br>
<input type="radio" name="category" value="tv">TV Posters<br>
<input type="radio" name="category" value="motor">Motor Posters<br />
<input type="radio" name="category" value="sports">Sports Posters<br><br />
Image Name :<br>
<input style="margin-left:10px;" id="uploadImage" required="required" name="pic" type="file" size="4
000000" accept="image/gif, image/jpeg, image/x-ms-bmp, image/x-png" > <br>
Image Price :<br>
<input type="text" placeholder="Rs xxx" name="imageprice" id="price"><br>
Image Description :<br>
<input type="text" name="imagedescription" id="description" height="300px"><br /><br>
<input type="submit" name="submit" value="Submit">
</div>
try like this,
$fileName = $_FILES['pic']['name'][0];

Multiple image upload

i have a problem in multiple image upload, when i upload, there's only 1 image which go to the targeted directory and database. i need two images to go directory and database...any help please?
this is my form
<form action="test.php" method="post" enctype="multipart/form-data">
Keywords: <input style="margin-left:35px;" type="text" name="keyword" /><br />
Name: <input style="margin-left:60px;" type="text" name="name" /><br />
Name2: <input style="margin-left:60px;" type="text" name="name2" /><br />
Categorie: <input style="margin-left:40px;" type="text" name="categorie" /><br />
Lieu: <input style="margin-left:70px;" type="text" name="lieu" /><br /><br />
<input type="file" name="file[]" multiple="multiple" /><input type="file" name="file_2[]" multiple="multiple"/><input type="submit" name="submit" value="Upload" />
</form>
<?php
$connect = mysql_connect("localhost", "root", "");
$select_db = mysql_select_db("yakatrouver_test", $connect);
if(#$_POST['submit']){
$keywords = $_POST['keyword'];
$name = $_POST['name'];
$name2 = $_POST['name2'];
$categorie = $_POST['categorie'];
$lieu = $_POST['lieu'];
$file = $_FILES['file'];
$file_name = $file['name'];
$file_type = $file['type'];
$file_size = $file['size'];
$file_path = $file['tmp_name'];
$file_2 = $FILES['file_2'];
$file_name_2 = $file['name'];
$file_type_2 = $file['type'];
$file_size_2 = $file['size'];
$file_path_2 = $file['tmp_name'];
if($file_name!="" && ($file_type="image/jpeg"||$file_type="image/png"||$file_type="image/gif") && $file_size<=2000000)
if(move_uploaded_file ($file_path, 'pictures_uploaded/' .$file_name))
$query = mysql_query("INSERT INTO `user_input`(keyword, name, categorie, lieu) VALUES ('$keywords', '$name', '$categorie', ' $lieu')");
$query = mysql_query("UPDATE `user_input` set image='pictures_uploaded/$file_name' WHERE `name`='$name'");
if($query == true)
{
echo "file Uploaded";
}
}
$result = mysql_query("SELECT * FROM `user_input` WHERE `image`=''") or die(mysql_error());
while($row = mysql_fetch_array($result))
?>
Try:-
HTML:
<input type="file" name="file[]"/>
<input type="file" name="file[]"/>
<input type="file" name="file[]"/>
PHP:
$file_count = count($_FILES['file']['name']);
for($i=0;$i<$file_count;$i++) {
$file_name = $_FILES['file']['name'][$i];
$file_type = $_FILES['file']['type'][$i];
$file_size = $_FILES['file']['size'][$i];
$file_path = $_FILES['file']['path'][$i];
if($file_name!="" && ($file_type="image/jpeg"||$file_type="image/png"||$file_type="image/gif") && $file_size<=2000000)
if(move_uploaded_file ($file_path, 'pictures_uploaded/' .$file_name))
$query = mysql_query("INSERT INTO `user_input`(keyword, name, categorie, lieu) VALUES ('$keywords', '$name', '$categorie', ' $lieu')");
$query = mysql_query("UPDATE `user_input` set image='pictures_uploaded/$file_name' WHERE `name`='$name'");
if($query == true)
{
echo "file Uploaded";
}
}
} //close the braces accordingly, some seem to be missing in your code
I think it's self-explanatory. It's just looping through your file input array, fetching each file information and processing one at at time.

Categories