Jump to content

Add incremental numbers to uploaded image filename - help!


tezza42

Recommended Posts

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);

 

};

Link to comment
Share on other sites

  • 3 weeks later...

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.

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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();

Link to comment
Share on other sites

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.

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.