I am working on a eCommerce based website which is selling products like T-shirts printing, Coffee Cup Printing. I want to upload multiple image files for multiple products but its not working i have attached a screenshot of my html form and also php code of that file.
Please help me to sort out this problem. Thanks in advance.
<?php
if($_POST)
{
$userid=$_SESSION['new_userid'];
$date = date("Y-m-d H:i:s");
foreach($_POST['productid'] as $row=>$productid)
{
$pid = mysql_real_escape_string($productid);
$quantity = mysql_real_escape_string($_POST['quantity'][$row]);
foreach($_FILES['file']['tmp_name'] as $key => $tmp_name )
{
$file_name = time().$_FILES['file']['name'][$key][$row];
$file_size =$_FILES['file']['size'][$key][$row];
$file_tmp =$_FILES['file']['tmp_name'][$key][$row];
$file_type=$_FILES['file']['type'][$key][$row];
if($file_size > 199097152){
$errors[]='File size must be less than 19 MB';
}
$desired_dir="uploads/orders/";
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,"$desired_dir/".$file_name);
}else{ // rename the file if another one exist
$new_dir="$desired_dir/".$file_name.time();
rename($file_tmp,$new_dir) ;
}
}else{
print_r($errors);
}
$tempArr[] = $file_name;
}
$files_360 .= implode('*',$tempArr);
//Project images
$insert = $DB_class->insert('orders',array("userid"=>$userid,"proid"=>$pid,"quantity"=>$quantity,"images"=>$files_360,"date"=>$date,"status"=>"pending"), '');
}
}
?>
I have solved this myself
<?php
if($_POST)
{
$a = 1;
$userid=$_SESSION['new_userid'];
$date = date("Y-m-d H:i:s");
foreach($_POST['productid'] as $row=>$productid)
{
$tempArr = "";
$pid = mysql_real_escape_string($productid);
$quantity = mysql_real_escape_string($_POST['quantity'][$row]);
foreach($_FILES['multifile'.$a]['tmp_name']['file'] as $key => $tmp_name )
{
$file_name = time().$_FILES['multifile'.$a]['name']['file'][$key];
$file_size =$_FILES['multifile'.$a]['size']['file'][$key];
$file_tmp =$_FILES['multifile'.$a]['tmp_name']['file'][$key];
$file_type=$_FILES['multifile'.$a]['type']['file'][$key];
if($file_size > 199097152){
$errors[]='File size must be less than 19 MB';
}
$desired_dir="uploads/orders/";
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,"$desired_dir/".$file_name);
}else{ // rename the file if another one exist
$new_dir="$desired_dir/".$file_name.time();
rename($file_tmp,$new_dir) ;
}
}else{
print_r($errors);
}
$tempArr[] = $file_name;
$files_360 = "";
}
$files_360 = implode('*',$tempArr);
//Project images
$insert = $DB_class->insert('orders',array("userid"=>$userid,"proid"=>$pid,"quantity"=>$quantity,"images"=>$files_360,"date"=>$date,"status"=>"pending"), '');
$a++;
}
}
?>
and here is my html code
<input type="file" name="multifile<?php echo $pro_id; ?>[file][]" multiple>