0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

phpをhtmlにxamppで出力する

Last updated at Posted at 2025-10-29
<?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";
            }
        }
    }
}
?>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?