Skip to content

Instantly share code, notes, and snippets.

@t-kashima
Created February 24, 2015 16:09
Show Gist options
  • Save t-kashima/d2ffd9e2d544fb2084ef to your computer and use it in GitHub Desktop.
Save t-kashima/d2ffd9e2d544fb2084ef to your computer and use it in GitHub Desktop.
<?php
/**
* WordpressのXMLから使われている画像をダウンロードする
*
* $argv[1] -> WordpressでエクスポートしたXML
* $argv[2] -> ダウンロードした画像の保存場所
*/
$xml_file_path = $argv[1];
$image_dir_path = $argv[2];
if (!$xml_file_path) {
return;
}
$content = file_get_contents($xml_file_path);
$pattern = '/http.+?\/wp-content\/.+?(?:.png|.jpg)/';
preg_match_all($pattern, $content, $matches);
if (0 >= count($matches)) {
return;
}
$image_url_list = $matches[0];
$file_name_pattern = '/[^\/]+?(?:.png|.jpg)/';
foreach ($image_url_list as $image_url) {
if (!preg_match($file_name_pattern, $image_url, $match)) {
continue;
}
$image_content = file_get_contents($image_url);
$file_name = $match[0];
$file_path = $image_dir_path . '/' . $file_name;
file_put_contents($file_path, $image_content);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment