Jump to content

creating a unique image name


ball420

Recommended Posts

Hey all, I'm trying to upload multiple image(which works great) but the problem I'm having is when an image is uploaded it over-rights a previous image if they are the same name and the old one is gone. Solution: create a unique name for each image before it's uploaded, change the name and then upload it. I can't seem to figure the correct syntax. My thought was to take the image name and add a random number to the front of it. Am i going about this the right way? thanks for the replies in advance. code below

 


if (isset($_POST['Submit'])) {

    $number_of_file_fields = 0;
    $number_of_uploaded_files = 0;
    $number_of_moved_files = 0;
//creating my random number
     $random_digit=rand(0000,9999);
     $uploaded_files = array();
     $upload_directory = dirname(__file__) . '/car-images/'; //set upload directory
  
    for ($i = 0; $i < count($_FILES['images']['name']); $i++) {
        $number_of_file_fields++;
        if ($_FILES['images']['name'][$i] != '') { //check if file field empty or not
            $number_of_uploaded_files++;
            $uploaded_files[] = $_FILES['images']['name'][$i];
            if (move_uploaded_file($_FILES['images']['tmp_name'][$i], $upload_directory . $_FILES['images']['name'][$i])) {
                $number_of_moved_files++;
            }

        }

    }


$image0 = $uploaded_files[0];
$image1 = $uploaded_files[1];
$image2 = $uploaded_files[2];
$image3 = $uploaded_files[3];
$image4 = $uploaded_files[4];


Link to comment
Share on other sites

you could append time() to it, that's the easiest solution I can think of in the moment.

 

Or the other way of going about this would be to check the file names of what you're uploading against what already exists, if it already exists regenerate a new random number to append to it and run the check again, keep looping until the file name isn't taken.

Link to comment
Share on other sites

here us small portion of script from my family album. You should be able to modify to your specific needs...

 

/* 
check to see if file already exists in the folder 
if it exists AND rename option is , create new name
*/

$does_it = FALSE;

$original_name = strtolower($original_name);

if(file_exists($uploadDir . $original_name)) {

if($image_overwrite == 1) {

	while(!$does_it) {

		$add_this_name = rand(1,200);

		$original_name = $add_this_name . $original_name;

		if(!file_exists($uploadDir . $original_name)) {

			$does_it = TRUE;

		}

	}

}

}

Link to comment
Share on other sites

I wanted to post the solution in case anyone ever needs it. What I ended up doing was creating a uniqid() and taking that unique id and changing the name in move_uploaded_file. but i needed these new unique names to be in an array so that i could INSERT into SQL. so what i did ws in my for loop everytime there was a new uniqid created i pushed it it the array. Sorry if it's confusing but you can look at my original code and compare to the working code below

 

    $number_of_file_fields = 0;
    $number_of_uploaded_files = 0;
    $number_of_moved_files = 0;

$un_id =uniqid();
$uploaded_files = array();
$final_names = array();

$upload_directory = dirname(__file__) . '/car-images/'; //set upload directory
    /**
     * we get a $_FILES['images'] array ,
     * we procee this array while iterating with simple for loop 
     * you can check this array by print_r($_FILES['images']); 
     */
    for ($i = 0; $i < count($_FILES['images']['name']); $i++) {
        $number_of_file_fields++;
        if ($_FILES['images']['name'][$i] != '') { //check if file field empty or not
            $number_of_uploaded_files++;

		//this adds to the unique id for each item in the array
		$un_id++;


            if (move_uploaded_file($_FILES['images']['tmp_name'][$i], $upload_directory . $un_id . ".jpg")) {
                $number_of_moved_files++;

			$the_new_names = $un_id . ".jpg";
			array_push($final_names, $the_new_names);

            }

        }

    }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.