getButterfly Logo getButterfly

I have recently had a situation on my hands, where attached PDFs had names like My-Document.pdf, and my-document.pdf would not work. While waiting for hosting support to fix it by activating the mod_speling module, here’s what I did:

First, I created a PHP file, called _renamer.php, and added this code:

$directory = 'wp-content/uploads/2014/02';

echo '<div>starting...</div>';

$files = scandir($directory);

foreach($files as $key => $name) {
$oldName = $name;
$newName = strtolower($name);
echo '<div>checking... ';
rename("$directory/$oldName", "$directory/$newName");
echo '' . $directory/$oldName . ' renamed to ' . $directory/$newName . '</div>';

The code above would rename all the files in the selected directory to lowercase. I only needed to change the directory path several times for each directory. Then, I ran

After the above operation was complete for all selected directories, I needed to update the post links and the post metas. The following SQL queries, run inside phpMyAdmin, will finish the process.

UPDATE wp_posts SET `guid` = LOWER(`guid`) WHERE post_type = 'attachment';
UPDATE wp_postmeta SET `meta_value` = LOWER(`meta_value`) WHERE meta_key = '_wp_attached_file';

Don’t forget to clear the cache, if using any plugin or CDN, and you’re done!

Subscribe to getButterfly Blog

Once a week or so we send an email with our best content. We never bug you, we just send you our latest piece of content.

If you found any value in this post, agree, disagree, or have anything to add - please do. I use comments as my #1 signal for what to write about. Read our comment policy before commenting! Comments such as "Thank you!", "Awesome!", "You're the man!" are either marked as spam or stripped from URL.

Leave a Reply

Your email address will not be published. Required fields are marked *