Renaming a file if it exists

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Anarking
Forum Newbie
Posts: 24
Joined: Wed Feb 11, 2009 11:29 am

Renaming a file if it exists

Post 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
Last edited by Benjamin on Mon May 04, 2009 5:46 pm, edited 1 time in total.
Reason: Changed code type from text to php.
Post Reply