Page 1 of 1

Renaming a file if it exists

Posted: Mon May 04, 2009 4:54 pm
by Anarking
Hi there


Im having a problem with this script

Code: Select all

$myFile = $dir.$names;
        
        /**
        while(file_exists($myFile)) 
        {
            $z = 1;
            echo "The file ".$myFile." exists"."<BR>";
            $myFile = $dir.$z.$names;
            echo $myFile;
            $z++;
        }
        **/
        
        if (file_exists($myFile)) 
        {
            $z = 1;
            echo "The file ".$myFile." exists"."<BR>";
            $myFile = $dir.$z.$names;
            
        } else 
            {
                echo "The file ".$name." does not exist"."<BR>";
            }

Basically I want to add an integer to a file name if it already exists such as test.txt, 1test,txt, 2test.txt

Unforteiuntly, obviously the If statement will only change it once... while the While loop leaves me in a n infinite loop