If file name exists...add 1 or add 2 to the end..

Discussion in 'PHP' started by timallard, Jan 29, 2008.

  1. #1
    Hello,

    1 more quick question.
    Im saving an XML file on my server...it now removes white space and non file name chars.

    If the file name already exists, it is overwriting.
    How can i have it increment, if exists..

    E.g.


    Saving:
    MyFileName

    On Server Already
    MyFileName

    Saves It As
    MyFileName1


    Thanks!
    -Tim
     
    timallard, Jan 29, 2008 IP
  2. maroc

    maroc Peon

    Messages:
    33
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you can use the function file_exists() to check if the file_exists()
     
    maroc, Jan 29, 2008 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    
    
    $file = 'somename';
    
    $int = 1;
    $tmp = $file;
    
    while (file_exists($tmp . '.xml'))
    {
        $tmp = $file . $int;
        $int++;
    }
    
    $final_name = $tmp;
    
    
    PHP:
     
    nico_swd, Jan 29, 2008 IP