PHPRO.ORG

This script will take a directory and recursively rename all files in it to safe filenames. A safe filename in this instance is regarded as having no spaces or any other non alpha-numeric character except for the _ underscore character.

Be warned, that once run, there is no turning back. In its current configuration it is used from the command line but could just as easily be used from the web. Simply change the new line characters to HTML line breaks.

The recursive part of the script is handled by the SPL RecursiveDirectoryIterator and the RecursiveIteratorIterator. The use of SPL iterators mean no recursive function is required in the user code as it is handled internally at a lower level.



<?php

/*** the target directory, no trailling slash ***/
$directory './';

try
    {
        
/*** check if we have a valid directory ***/
        
if( !is_dir($directory) )
        {
            throw new 
Exception('Directory does not exist!'."\n");
        }

        
/*** check if we have permission to rename the files ***/
        
if( !is_writable$directory ))
        {
            throw new 
Exception('You do not have renaming permissions!'."\n");
        }

    
        
/**
        *
        * @collapse white space
        *
        * @param string $string
        *
        * @return string
        *
        */
        
function collapseWhiteSpace($string)
        {
            return  
preg_replace('/\s+/'' '$string);
        }

        
/**
        * @convert file names to nice names
        *
        * @param string $filename
        *
        * @return string
        *
        */
        
function safe_names($filename)
        {
            
$filename collapseWhiteSpace($filename);
            
$filename str_replace(' ''-'$filename);
            
$filename preg_replace('/[^a-z0-9-.]/i','',$filename);
            return  
strtolower($filename);
        }

        
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory0));
        
/*** loop directly over the object ***/
        
while($it->valid())
            {
            
/*** check if value is a directory ***/
            
if(!$it->isDot())
            {
                if(!
is_writable($directory.'/'.$it->getSubPathName()))
                {
                    echo 
'Permission Denied: '.$directory.'/'.$it->getSubPathName()."\n";
                }
                else
                {
                    
/*** the old file name ***/
                    
$old_file $directory.'/'.$it->getSubPathName();

                    
/*** the new file name ***/
                    
$new_file $directory.'/'.$it->getSubPath().'/'.safe_names($it->current());
                    
                    
/*** rename the file ***/
                    
rename ($old_file$new_file);

                    
/*** a little message to say file is converted ***/
                    
echo 'Renamed '$directory.'/'.$it->getSubPathName() ."\n";
                }
            }
            
/*** move to the next iteration ***/
            
$it->next();
        }
        
        
/*** when we are all done let the user know ***/
        
echo 'Renaming of files complete'."\n";
    }
    catch(
Exception $e)
    {
        echo 
$e->getMessage()."\n";
    }
?>