PHP bulk rename, with more than one renaming function
<p>I'm new to PHP and I'm trying to do something similar to what user580950 asked about at <a href="http://stackoverflow.com/questions/4993590/bulk-rename-files-in-a-folder-php">Bulk Rename Files in a Folder - PHP</a> .   </p>

<p>I want to write a script that will iterate over the names of all the files and directories in a given directory and do two things: replace the spaces with dashes and convert all caps to lowercase.</p>

<p>Based on the answer in the aforementioned question, and the PHP manual entries for the required functions, I came up with the following code:</p>

<pre><code>if ($handle = opendir('/Users/username/Documents/School')) {

    while (false !== ($file_name = readdir($handle))) { 
        $to_lower = strtolower($file_name);
        $add_dashes = str_replace(" ", "-", $to_lower);
        rename($file_name , $add_dashes);
    }

    closedir($handle);
}
</code></pre>

<p>This code returns the following error for every single file/directory in the target directory:</p>

<blockquote>
  <p>Warning: rename(THE 273,the-273): No such file or directory in
  /Users/username/Sites/PHP/rename_files_in_directory.php on line 8</p>
</blockquote>

<p>I've tried rearranging things in all kinds of ways, and I am completely stumped as to where the trouble is. I'm running PHP 5.3.8 on Mac OSX.6.8 . </p>

<p>Help will be greatly appreciated!</p>
