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";
                }
            }
        } 
    }
    ?>
  1. You move te uploaded file before you check it exist.
  2. you rename the already remaned file creating something like: image_0_1_2.jpg

Hello,

Without writing the code for you basically what you need to do is add a section of code that looks for a record with the image name. If it returns a row then there is an image and show error if not continue processing.

$unique_id = $dbh->quote($_GET['unique_id']);
$sth = $dbh->query("SELECT * FROM database WHERE unique_id = $unique_id");
if ($sth->numRows( )) {
// already submitted, throw an error
} else {
// act upon the data
}

Check the below link
Click Here

Hi guys thanks for all the responds ... @pzuurveen I did moved the

move_uploaded_file($tmp_name, "$target/$name");

under

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;
}
}
}

but still not what I need. it still overwrite my files when uploading.

hi @rch1231 no what I need is if file exists change it's name and put it with the new name into my db and my target folder so no dublication in db and no overwrite in the folder.
Thanks

You change the use the filename that you already changed as the oridanal one. $filename_data=explode(".", $_FILES['img']['name']);
The solustion is to take this out of the while loop.
You also need to test if the file exist in the $target dir as you do inside the loop

check it.

$filenamekey = md5(uniqid($_FILES["myfile"]["name"], true)); 

move_uploaded_file($_FILES["myfile"["tmp_name"],$output_dir.$filenamekey); 
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.