SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Check if a file exists and rename another file if the file exists

    PHP Code:
    <?
        
        
    if (file_exists($path))
        {
            
    $i1;
            
            while (
    file_exists($path))
            {
                
    // get file extension
                
    $extension pathinfo($pathPATHINFO_EXTENSION);
                
                
    // get file's name
                
    $filename pathinfo($pathPATHINFO_FILENAME);
                
                
    // add and combine the filename, iterator, extension
                
    $new_filename $filename '-' $iterator '.' $extension;
                
                
    // add file name to the end of the path to place it in the new directory; the while loop will check it again
                
    $path $directory $new_filename;
                
    $i++;
                
            }
        }
    ?>
    This code is supposed to rename a file, if it exists, to the name of that file with a hypen and a number. Example: "word_document.doc" to "word_document-1.doc". And if word_document-1.doc exists, rename it to word_document-2.doc, and so on. Instead, if a file exists, I get word-document-1.doc (works the first time) but then it just adds the iterator instead of substituting it for files that follow with the same name. So I get word_document-1-2.doc, and word_document-1-2-3.doc. I think I can see why this is happening, the new file variable contains the previous while loop's iterator, but I don't know how to fix it without using some regex to detect the previous iterator, and I think that is probably not needed here. Please help.

  2. #2
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    You'll need to save the new path outside of the loop so it can be referenced and checked.

    Something like this:

    Code:
    if (file_exists($path))     { 
            $i=1; 
    		$new_path=$path;
             
            while (file_exists($new_path)) 
            { 
                // get file extension 
                $extension = pathinfo($path, PATHINFO_EXTENSION); 
                 
                // get file's name 
                $filename = pathinfo($path, PATHINFO_FILENAME); 
    			
    			// get file's directory
    			$directory=dirname($path);
                 
                // add and combine the filename, iterator, extension 
                $new_filename = $filename . '-' . $i . '.' . $extension; 
                 
                // add file name to the end of the path to place it in the new directory; the while loop will check it again 
                $new_path = $directory . $new_filename; 
    			
                $i++;  
            } 
    		
    		if(!rename($path, $new_path)){
    			echo 'error renaming file';
    		}
        }
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just wanted to say thank you for this. It really helped.

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,039
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    No need to define $directory, $filename and $extension in the while loop, as they will never change for different iterations.
    Also, you only need to rename the file if it actually needs to be renamed, otherwise there is no need to bother.

    PHP Code:
    list($directory, , $extension$filename) = array_values(pathinfo($path));

    $i 0;
    while (
    file_exists($path))

        
    $new_path $directory '/' $filename '-' $i '.' $extension;
        
    $i++;


    if (
    $path !== $new_path && !rename($path$new_path))
    {
        throw new 
    Exception('Error renaming `'.$path.'` to `'.$new_path.'`');

    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's a good catch. I am always looking to improve my loop performance, so thanks for the update.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •