Skip to content

Instantly share code, notes, and snippets.

@tommymarshall
Last active April 21, 2016 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommymarshall/279d238d9341d68af65e5a458bef8b61 to your computer and use it in GitHub Desktop.
Save tommymarshall/279d238d9341d68af65e5a458bef8b61 to your computer and use it in GitHub Desktop.
Move WordPress Generated Files to ./thumbnails/ folder

Move WordPress Generated Files to ./thumbnails/ folder

Keeping WordPress Media Library updated even after a server move.

Instructions

  1. Place the move.php script inside of a folder containing images containing both the Original and WordPress auto-generated thumbnails.
  2. Run the script by navigating to it in the browser. All WordPress generated images will be moved to the ./thumbnails/ subfolder, all remaining images in your original folder are original images.
  3. Upload all Original images to your server.
  4. Install Add From Server Plugin to WordPress.
  5. Select all original images within the Add From Server interface and import.
  6. Upload all Thumbnail images to server as well.
<?php
$files = find_all_files(".");
$thumbnails = [];
foreach($files as $key => $file)
{
$pathinfo = pathinfo($file);
if (preg_match("/(\S*)-(\d*)x(\d*).(\S*)/", $file))
{
$thumbnails[] = $file;
rename($file, './thumbnails/'.$file);
}
else if ($pathinfo['extension'] != 'php')
{
$originals[] = $file;
}
}
echo '<h1>Thumbnails</h1>';
echo '<pre>';
var_dump($thumbnails);
echo '</pre>';
echo '<h1>Originals</h1>';
echo '<pre>';
var_dump($originals);
echo '</pre>';
function find_all_files($dir)
{
$root = scandir($dir);
foreach($root as $value)
{
if($value === '.' || $value === '..') {continue;}
if(is_file("$dir/$value")) {$result[]="$dir/$value";continue;}
foreach(find_all_files("$dir/$value") as $value)
{
$result[]=$value;
}
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment