uploading multi-images without name duplication

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Yousef Altaf
    New Member
    • Jun 2011
    • 22

    uploading multi-images without name duplication

    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

    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";
    			}
    		}
    	} 
    }
    ?>
  • vspiridonov
    New Member
    • Jan 2014
    • 2

    #2
    You can just add mktime() value to every image name.

    Comment

    • Yousef Altaf
      New Member
      • Jun 2011
      • 22

      #3
      vspiridonov can you please send me an exsample on what you are saying

      Comment

      • vspiridonov
        New Member
        • Jan 2014
        • 2

        #4
        Code:
        $new_filename=$filename_data[0] . "_" . mktime() . "." . $filename_data[1];

        Comment

        Working...