tezza42 Posted May 15, 2010 Share Posted May 15, 2010 All I want is to be able to add a number, preferable in brackets, to the end of the filename but can't for the life of me, find out how to do it. Have searched for days on google and sifted through hundreds of websites but I can't seem to get a straight answer! Users are allowed to upload up to 30 image on our site (one at a time) for each property they have listed. Each property has its own id number when they initially register it. When a user uploads an image the image filename is currently changed to: (The property id)(hyphen)(date/time)(dot)(jpg) Example: 81-150520101530.jpg The above example is for an image file uploaded for property with id 81 on 15th May 2010 at 15:30 The major drawback with this is if a user manages to upload 2 photos within the same minute using (dmYHi) the first image is overwritten. If I add seconds to the filenme for example; (dmYHis) sometimes there will be an error and some images won't show because in the MySQL database the file may say: 81-15052010153022.jpg but in the image upload folder the image can sometimes be 1 second out example: 81-15052010153023.jpg There has been a difference of 1 second between the image being uploaded to the image folder and the name being inserted in the MySql datebas. Hence the not showing because it does not match the database filename. To solve this, short term, I have been using the rand() function like this rand(0,30) but I really really do not want to use this. Want I want is to add incremental numbers to each photo uploaded but haven't a clue how to do it. Please help anyone! I would like it like this: 1st photo name = 81-[1].jpg 2nd photo name = 81-[2].jpg 3rd photo name = 81-[3]jpg 4th photo name = 81-[4].jpg 5th photo name = 81-[5].jpg etc etc etc up to and including 81-[30].jpg It doesn't matter about the date but what I also need to keep in mind that the user may come back at a later time and upload more images for this property. Also user may delete certain images of the property at any time to change for new ones. Lets say they delete... 3rd photo name = 81-[3]jpg 4th photo name = 81-[4].jpg and upload 2 different images... Because [1],[2] and [4]to[30] already exist these two new image should be named: 81-[3]jpg 81-[4].jpg AGAIN. Current code looks like this: } elseif ($_REQUEST["do"]=='add_photo') { $ref=rand(0,30); $temp = upload_image($HTTP_POST_FILES['photo']['tmp_name'], $_REQUEST["id"].'-'.date('dmYHi').'['.$ref.'].jpg', $OPTIONS["path_accommodations"], $OPTIONS["pic_width"],$OPTIONS["pic_height"], ''); if ($temp==1) { $sql = "INSERT INTO ".$TABLES["accommodations_photos"]." SET accommodation_id='".$_REQUEST["id"]."', filename='".$_REQUEST["id"].'-'.date('dmYHi').'['.$ref.'].jpg'."', description='".mysql_escape_string($_REQUEST["description"])."'"; $sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql); }; Quote Link to comment https://forums.phpfreaks.com/topic/201896-add-incremental-numbers-to-uploaded-image-filename-help/ Share on other sites More sharing options...
siva.katir Posted June 6, 2010 Share Posted June 6, 2010 I'm curious why you chose to use the date as part of the file name? When I wrote my file unloader (which can pass as many files up to 100mb per post as the user wants) I specifically chose not to because of this exact issue. Same with using a random number, you can have the off chance of duplicating files (granted it's crazy unlikely). My solution was to use the auto increment function in mySQL. It looks like you are inserting one image into one line of the db, is that correct? If so you can do this: $query = "INSERT INTO blah blah blah some cells to create the mySQL row"; mysql_query($query); $id = mysql_insert_id(); $id becomes the row key that mySQL just created Now that you have the id, create the name you want: $file_name = $property-id-variable."-".$id.".".$ext; Now run an UPDATE query: $query = "UPDATE your-table SET file_name='".$file_name." If mySQL doesn't return the ID# $id is set to 0. It's not as clean as a solution as you're forced to run multiple queries per upload, but it's very fail safe. Quote Link to comment https://forums.phpfreaks.com/topic/201896-add-incremental-numbers-to-uploaded-image-filename-help/#findComment-1068655 Share on other sites More sharing options...
teynon Posted June 6, 2010 Share Posted June 6, 2010 One of the biggest issues I've found is the date issue. Not only might it be a bit off, but apparently, MySQL and PHP handle daylight savings time in different time zones (specifically where I am right now [iraq]) differently. The solution I have found for this is to pick one or the other. Either all times are based off of MySQL or PHP. You can base it off of MySQL by doing a "SELECT CURRENT_TIMESTAMP" or you can fill in timestamps in MySQL with your PHP generated time. But i'm going a little off topic and not really helping your initial problem. Can we see some code for this? Quote Link to comment https://forums.phpfreaks.com/topic/201896-add-incremental-numbers-to-uploaded-image-filename-help/#findComment-1068658 Share on other sites More sharing options...
calmchess Posted June 6, 2010 Share Posted June 6, 2010 there are plenty of ways to generate a unique file name here is one just off the top of my head probably needs tweaked whats wrong with this? the file name is a little long but it pretty much garuntees a unique number $newfilename=rand(md5($filename))/rand(); Quote Link to comment https://forums.phpfreaks.com/topic/201896-add-incremental-numbers-to-uploaded-image-filename-help/#findComment-1068677 Share on other sites More sharing options...
calmchess Posted June 6, 2010 Share Posted June 6, 2010 here one that works $newfilename = md5(uniqid(rand(),true)); Quote Link to comment https://forums.phpfreaks.com/topic/201896-add-incremental-numbers-to-uploaded-image-filename-help/#findComment-1068679 Share on other sites More sharing options...
ignace Posted June 6, 2010 Share Posted June 6, 2010 81-15052010153022.jpg but in the image upload folder the image can sometimes be 1 second out example: 81-15052010153023.jpg date() re-calculates the date/time every time it is called that's why you need to store it's output if you wish to use it in multiple places that rely on each other of being equal. Quote Link to comment https://forums.phpfreaks.com/topic/201896-add-incremental-numbers-to-uploaded-image-filename-help/#findComment-1068694 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.