Cleanup uploads folder, Media Library db structure

I am doing an upgrade and cleanup of an old blog that started on Blogger.com, then moved to self-hosted WordPress 2.1, 2.3 and now 3.0. Images in the posts are scattered around: most of them are in /wp-content/, but not all in /wp-content/uploads/ or /wp-content/uploads/year/month/. Others still reference the blogger.com image servers. I want to consolidate them all in /wp-content/uploads/year/month/, which I think is the “correct” place? This should make later upgrades easier.

What is the best way to do this? I would like to update the metadata too, so that the Media Library can manage all of them. I could not find any documentation on this, so sorry if I missed it, please point me to it.

Of course, I would provide redirects from the old locations to the new. What is the best way to do this? A template 404.php, of a plugin? I would like to add some logging, so we can find the referring pages and maybe correct them.

(This question was originally asked on the WordPress support forums, but got no useful answer there).

Solutions Collecting From Web of "Cleanup uploads folder, Media Library db structure"

W3-Total Cache has a feature that will scan all your posts and import any external images into the media library and change the paths to the new location.

See Screenshot

alt text

The correct place is the current setting I would say. So if you now use one upload folder (instead of years / month subfolders) go with that one (might get very full, but easier to sort. So just moving all files from the year / month subdirectories into the uploads directory and changing the URL-Paths in the database should normally do the job.

After all is in one folder (and sorted), you can change the folder design again, and move files into the correct directory with a PHP script that reads out the date from database. The exact same script needs to adopt URL paths again as well.

That’s something like the exact same script that mike suggested.

Right now, the WP UI does not support to manually move or rename files, so you’re bound to some own code. Which would make sense for a batch anyway.

Existing and Related Plugins

  • Add From Server (WordPress Plugin) – “Add From Server” is a quick plugin which allows you to import media & files into the WordPress uploads manager from the Webservers filesystem

@Jan Fabry: You are probably going to need to tackle each case separately, i.e. the Blogger hosted images and the ones outside wp-content, etc.

Here’s a plugin for importing from Blogger; it hasn’t been updated in a while but may still work or at least have code you can use:

  • Blogger Image Import Plugin (notes)

There’s also an older plugin that may or may not still work:

  • Import Lagacy Media

Beyond that I think you’ll just need to write a PHP script that scans your files and scans your posts looking for embedded image URLs, develops an intersected list and then moves the files and updates the reference in the post. Or maybe you’ll get lucky and that plugin will still work and do it for you!

Good luck.

Maybe (and that’s just for future readers – it’s an old Q) you can list all ‘post_type’ => ‘attachment’ and start separating from there. I mean fetching the posts and saving the different locations in one array. Then sort this out to get an overview of where your images are lying around…