0

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";
                }
            }
        } 
    }
    ?>
5
Contributors
7
Replies
30
Views
3 Years
Discussion Span
Last Post by anas.man
0
  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

Edited by pzuurveen

0

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

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.

0

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

0

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

1

check it.

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

move_uploaded_file($_FILES["myfile"["tmp_name"],$output_dir.$filenamekey); 
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.