Forum Moderators: coopster

Message Too Old, No Replies

Add incremental numbers to uploaded image filename - help!

         

zorro

7:49 pm on May 15, 2010 (gmt 0)

10+ Year Member



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

};

Readie

10:28 pm on May 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why not have date('U'); instead of date('YmdHi') - it'll only be 10 characters long for the next, like, 270 odd years, and it'll be unique every second, rather than every minute.

You'll also be able to take it and use it directly in a date function to format it into the exact date/time:

$time = date('U');
echo date('YmdHis', $time);

zorro

6:19 pm on May 20, 2010 (gmt 0)

10+ Year Member



Thanks for the reply Readie!
The reason I wanted the images to have a incremental number added to them was so that I could reference them individually for dynamic rss feeds I have.
In the rss feed code which I am building for google maps I currently reference only one image like this...
http://www.example.com/accommodations/$row[filename]
(accommodations being the folder on the server the image is stored in)
($row[filename] being the field in the database)
This take only the first image for this property_id from the database
For example there may be 5 images in database for a property like...
id----property_id----------filename
21--------89-----------89-22052010191559.jpg
22--------89-----------89-22052010191622.jpg
23--------89-----------89-22052010191712.jpg
24--------89-----------89-22052010191752.jpg
25--------89-----------89-22052010191827.jpg
And I am looking for some way to dynamically reference the images above with id 22,23,24,25 too! But I have just realised adding an incremental number to the end of the filename will not make a blind bit of difference!
I am wanting to do it dynamically, so I can't be referencing the id itself - plus these may change as users delete and/or upload new/different images.
Maybe there is a way to reference all the images for property_id 89 but i'm not sure how?

Alcoholico

9:30 pm on May 20, 2010 (gmt 0)

10+ Year Member




$images_dir = '/a/b/c/';
$base_image_name = $property_owner_id;
$i = 1;
$new_img_name = $base_image_name.'-['.$i.'].jpg';
while(is_file($images_dir.$new_img_name)) {
$new_img_name = $base_image_name.'-['.$i.'].jpg';
$i++;
}

Notice I have dropped image date, it's confusing, and you can always use filemtime() to find out if you really need to know, not saying it is not possible doing it with the date, it is just slightly more complicated.

Readie

11:12 pm on May 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why don't you get a list of all the property_ids...

$sql = 'SELECT DISTINCT(property_id) FROM image_table ORDER BY property_id DESC'

Then you can loop through that list getting all the files for each particular id:
$results = array();
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)) {
$key = 'a' . $row['property_id'];
$results[$key] = array();
$sql = 'SELECT filename FROM image_table WHERE property_id = "' . $row['property_id'] . '" ORDER BY filename DESC';
$result2 = mysql_query($sql);
if($rows = mysql_num_rows($result2)) {
for($i = 0; $i < $rows; $i++) {
$results[$key][] = mysql_result($result2, $i, "filename");
}
}
}
echo '<pre>';
print_r($results);
echo '</pre>';
Will develop an associative array, more just to show you than anything else, as I'm a bit unsure what you mean by dynamic above - it's such a broad term.

zorro

11:55 am on May 22, 2010 (gmt 0)

10+ Year Member



Many thanks Alcoholico and Readie.
I will try them!
Readie what I meant by dynamic (i suppose) was to be able to implement some sort of code and be able to leave it after that. So if users delete or update pictures/image files the new images or the new amount of images show without further intevention on my part to any coding.

Once again guys or gals I thank you very much for your help!

zorro

3:32 pm on May 22, 2010 (gmt 0)

10+ Year Member



Hi there again,

Readie I tried using your example date('U') but I am coming across the same problems as I did when using date ('YmdHis')

For some strange reason the odd image will not upload.
This is because there is a very small difference in the file name which is sent to the database and the filename of the image in the image folder.
When I use ('YmdHis') on the odd occasion the filename in the database could be 22052010171538 (22nd May 2010 at 17:15 and 38 seconds)
but the filename in the image folder will say: 22052010171539 (a 1 second difference) so because the filename in the database and image folder do not match the image does not show in the users area.

Using your suggestion I uploaded 3 images (1 at a time is what the system allows) 2 uploaded ok both having the same unique numbers in the database and image folder.
The 3rd image uploaded said this...
Database: 5-1274541645.jpg
Image folder: 5-1274541644.jpg
It appears as though the image has been uploaded microseconds earlier to the image folder than the database?

Is there a solution to this?

Thanks in advance!

Readie

4:04 pm on May 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, we can go into microseconds on the name:

$time = gettimeofday();
$new_file_name = $time['sec'] . $time['usec'];


Gives a lot more margin for error.