I am trying to upload multi-images I am using this script everything is fine and the images uploaded successfully the problem here is I don't want to duplicate the images name so I used this script to avoid this but no luck
any idea
this is my code
<?php
if(isset($_POST['submit'])){
$target = '../images/Projects/';
$num=1;
$projectID=$p;
foreach ($_FILES["img"]["error"] as $key => $error){
if ($error==UPLOAD_ERR_OK){
$tmp_name = $_FILES["img"]["tmp_name"][$key];
$name = $_FILES["img"]["name"][$key];
move_uploaded_file($tmp_name, "$target/$name");
if(file_exists($name)){
$duplicate_filename = TRUE;
$i=0;
while($duplicate_filename){
$filename_data=explode(".", $_FILES['img']['name']);
$new_filename=$filename_data[0] . "_" . $i . "." . $filename_data[1];
$_FILES['img']['name']=$new_filename;
$name="$target".$_FILES['img']['name']."";
if(file_exists($name))
{
$i++;
}
else
{
$duplicate_filename=FALSE;
}
}
}
$putData = "INSERT INTO projects_images (id, image, image_id)VALUE('', '$name', '$projectID')";
$result = $db->query($putData)or die($db->error);
if($result){header('Location:index.php?id=2&proid=3&p='.$p.'&msg=Images has been uploaded successfully, carry on upload more images if you like');
}else{echo"Error";
}
}
}
}
?>