Add incremental numbers to uploaded image filename - help!

Discussion in 'PHP' started by texastez, May 15, 2010.

  1. #1
    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);

    };
     
    texastez, May 15, 2010 IP
  2. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #2
    looks like this, you said a user can upload 30pics at a time right. This means you will loop through each of the uploaded files. so you only need the timestamp of the first file then increment it.
    like:
    
    if(!empty($_FILES))
    {
      $timestamp = date('dmYHi');
      foreach($_FILES['somename']['name'] as $k => $file) {
        $timestamp ++;
        // ..
        filename='".$_REQUEST["id"].'-'.$timestamp .'['.$ref.'].jpg'."', 
        // .
      }
    }
    
    PHP:
     
    gapz101, May 15, 2010 IP
  3. texastez

    texastez Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    No user can not upload 30 pics at a time.
    User can only upload 1 pic at a time with a max of 30 pics altogether.
    Plus I do not want to include the date!
     
    texastez, May 15, 2010 IP
  4. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #4
    but timestamp is best
     
    gapz101, May 15, 2010 IP
  5. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #5
    there are many ways to do this one.. the data is stored into a database, create a query that returns the total count of the user uploaded images.
    Note : make sure you create a query that has less memory process, the table would probably go onto millions of rows. Read more on querying this is the important part.
    create the filename : use underscore userid_currentImageCount_dateTime.fileformat

    or also you can create a directory for the user, this is less secured but the solution will be the same. Parse the user folder and count the number of images. blah blah blah do your process blah blah....

    in case of deleting just simple delete query or create a column in the table and name it to flag(boolean if delete or not)
     
    bartolay13, May 15, 2010 IP