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