WEB Is there a function to make a filename unique with PHP?

Discussion in 'OT Technology' started by jdw, Feb 17, 2008.

  1. jdw

    jdw New Member

    Joined:
    Dec 31, 2005
    Messages:
    4,429
    Likes Received:
    0
    Location:
    ND
    It's so easy in Cold Fusion... :hsd:


    otherwise what, strip the extension and keep incrementing a variable until file_exists returns false?
     
  2. jdw

    jdw New Member

    Joined:
    Dec 31, 2005
    Messages:
    4,429
    Likes Received:
    0
    Location:
    ND
    well FWIW...

    I'm seeing now why people at work like Cold Fusion. So many quick shortcuts on things...

    Anyway, wrote a quick PHP function that removes bad characters and makes the filename unique... Still wondering if there's a shortcut in PHP I'm not finding...
     
  3. TheDarkHorizon

    TheDarkHorizon \xC0\xFF\xEE

    Joined:
    Sep 26, 2002
    Messages:
    2,396
    Likes Received:
    0
    Location:
    San Francisco, CA
  4. lukin87

    lukin87 New Member

    Joined:
    Jan 11, 2008
    Messages:
    135
    Likes Received:
    0
    php.net -> Search all php.net sites: create file unique name.

    first result.

    php has tons of build in stuff to make life a lot easier. I haven't touched coldfusion yet but it does sound interesting too.
     
  5. jdw

    jdw New Member

    Joined:
    Dec 31, 2005
    Messages:
    4,429
    Likes Received:
    0
    Location:
    ND
    The thing I gathered about the function you both posted is that it's a randomly generated name (same name that is given to an uploaded temp file before it gets moved to a directory)...
    I wanted something that'd add a numeric value to the end of the base filename...
    Cold Fusion has a function - Make Unique or something - don't recall the syntax - that'll do it all for you with the specified filename and directory.
     
  6. RaginBajin

    RaginBajin Have you punched a donkey today?

    Joined:
    Dec 24, 2001
    Messages:
    8,740
    Likes Received:
    0
    Location:
    NoVA
    Why don't you just take take the hour, minute and second. That's pretty unique, if you are going run into still issues then use nanosecond. Doesn't seem like you need a big function for that.
     
  7. jdw

    jdw New Member

    Joined:
    Dec 31, 2005
    Messages:
    4,429
    Likes Received:
    0
    Location:
    ND
    It'd be possible to go that route, but that means adding 6 or 8 characters to the filename instead of 1 or 2 and the only way to make it truly unique is to add a full day/month/year to that. And yes I'd rather make a filename unique vs making a new filename...

    None the less, I've already written a function... It's all of maybe 35 lines.. half of which are comments and it also removes bad characters.
     
  8. exify

    exify Active Member

    Joined:
    Nov 10, 2006
    Messages:
    3,168
    Likes Received:
    0
    Location:
    brooklyn, nyc
    mind PMing me that?
     
  9. jdw

    jdw New Member

    Joined:
    Dec 31, 2005
    Messages:
    4,429
    Likes Received:
    0
    Location:
    ND
    when i get home i will....
     
  10. jdw

    jdw New Member

    Joined:
    Dec 31, 2005
    Messages:
    4,429
    Likes Received:
    0
    Location:
    ND
    Code:
    <?
    //Function call needs to contain the filename to make unique (no path included) and the path to check in
    function make_filename_unique($filename, $uploaddir){
             //strip extension
    
             //Explodes $filename into an array
             $exploded_filename_array = explode('.', $filename);
    
             //sets filename extension from last array cell to variable $filename_extension
             $filename_extension = $exploded_filename_array[count($exploded_filename_array) - 1];
             
             //deletes last array cell which contains extension
             unset($exploded_filename_array[count($exploded_filename_array) -1]);
    
             //combines all remaining array cells - any other '.' will have been removed.  All other bad characters remain.
             $dirty_filename_base = implode("", $exploded_filename_array);
             
             //replaces or removes bad characters in dirty_filename_base
             $bad_char = array(" ", "!", "&", "-", "+", "%");
             $char_replace = array("_", "", "", "_", "", "");
             $clean_filename_base = str_replace($bad_char, $char_replace, $dirty_filename_base);
             
             //makes a unique filename
             $variable = "";  //this will be incremented to create the unique name
             while (file_exists($uploaddir.$clean_filename_base.$variable.".".$filename_extension)){
                 if ($variable == ""){
                     $variable = "1";
                 }else{
                     $variable++;
                 }
             }
             $unique_filename = $clean_filename_base.$variable.".".$filename_extension;
             return $unique_filename;
    
    }
    ?>
    
     

Share This Page