Skip to content

Instantly share code, notes, and snippets.

@steverobbins
Last active May 10, 2017 10:09
Show Gist options
  • Save steverobbins/e6bbb8c6272f652cfcf0 to your computer and use it in GitHub Desktop.
Save steverobbins/e6bbb8c6272f652cfcf0 to your computer and use it in GitHub Desktop.
Download product media images to your local Magento store without rsync/ftp.
<?php
$range = array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9), array('_'));
foreach ($range as $a) foreach ($range as $b) mkdir("$a/$b", 0777, true);
$db = new PDO('mysql:127.0.0.1;dbname=magento', 'root', 'root');
//$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$result = $db->query('select distinct value from catalog_product_entity_media_gallery');
foreach ($result as $row) {
$path = dirname(__FILE__) . '/' . trim($row['value'], '/');
if (!file_exists($path)) {
echo "{$row['value']}\n";
exec("curl -s -o $path http://static.example.com/media/catalog/product{$row['value']}");
}
}
@steverobbins
Copy link
Author

mkdir -p ~/html/magento/media/catalog/product
cd ~/html/magento/media/catalog/product
curl -o magento-fetch-media.php https://gist.githubusercontent.com/steverobbins/e6bbb8c6272f652cfcf0/raw/668643e3f3009ff959de5a8cb25627ffc46a4b54/magento-fetch-media.php
nohup php magento-fetch-media.php &

@PhilHalf
Copy link

Thank you so much for this, it has saved me a huge amount of work and was so simple to use!

There were a couple of changes I had to make to fit my setup in case it's of use to anybody else:

  1. Add '-' (hyphen) to the array of folders created e.g.:
    $range = array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9), array('_', '-'));

  2. Add the "host=" parameter back into the PDO constructor (running PHP 5.6.25):
    $db = new PDO('mysql:host=127.0.0.1;dbname=magento', 'root', 'root');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment