<?php
// ------------------------------------
// 設定
// ------------------------------------
$folders = ['us', 'jp']; // PHPフォルダ
$outputDir = __DIR__ . '/output'; // HTML出力先
$localhostRoot = 'http://localhost/myproject'; // XAMPPでアクセスするURLのルート
// ------------------------------------
// メイン処理
// ------------------------------------
foreach ($folders as $folder) {
$src = __DIR__ . '/' . $folder;
$dst = $outputDir . '/' . $folder;
recurseCopyPHP($src, $dst, '/' . $folder);
}
echo "✅ 全てのPHPをHTML化しました。\n";
// ------------------------------------
// 再帰コピー関数
// ------------------------------------
function recurseCopyPHP($src, $dst, $relativePath = '') {
if (!file_exists($dst)) mkdir($dst, 0777, true);
$items = scandir($src);
foreach ($items as $item) {
if ($item === '.' || $item === '..') continue;
$srcPath = $src . '/' . $item;
$dstPath = $dst . '/' . $item;
if (is_dir($srcPath)) {
// ディレクトリなら再帰
recurseCopyPHP($srcPath, $dstPath, $relativePath . '/' . $item);
} else {
$ext = pathinfo($item, PATHINFO_EXTENSION);
if ($ext === 'php') {
// PHPをレンダリングしてHTML化
$url = 'http://localhost/myproject' . $relativePath . '/' . $item;
$html = file_get_contents($url);
if ($html === false) {
echo "⚠️ 取得失敗: $url\n";
} else {
$dstPath = preg_replace('/\.php$/', '.html', $dstPath);
file_put_contents($dstPath, $html);
echo "HTML化: $dstPath\n";
}
} else {
// PHP以外はそのままコピー
copy($srcPath, $dstPath);
echo "コピー: $dstPath\n";
}
}
}
}
?>
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme