ball420 Posted January 11, 2012 Share Posted January 11, 2012 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]; Quote Link to comment https://forums.phpfreaks.com/topic/254759-creating-a-unique-image-name/ Share on other sites More sharing options...
MasterACE14 Posted January 11, 2012 Share Posted January 11, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/254759-creating-a-unique-image-name/#findComment-1306303 Share on other sites More sharing options...
litebearer Posted January 11, 2012 Share Posted January 11, 2012 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; } } } } Quote Link to comment https://forums.phpfreaks.com/topic/254759-creating-a-unique-image-name/#findComment-1306415 Share on other sites More sharing options...
ball420 Posted January 13, 2012 Author Share Posted January 13, 2012 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); } } } Quote Link to comment https://forums.phpfreaks.com/topic/254759-creating-a-unique-image-name/#findComment-1307286 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.