0

サーバーには、php スクリプトで変更する必要があるファイルがたくさんあります。ファイルコンテンツの前後にhtmlタグを追加したい。

4

1 に答える 1

0

あなたの質問は明確ではありません。基本的に、これを行うことができます:

$file_name = 'something';
$old_content = file_get_contents($file_name);

$html_before = '<html>... something before the old content';
$html_after = '</html>... something after the old content';

$result = $html_before . $old_content . $html_after;

file_put_contents($file_name, $result); // overwrite the original file. make sure you have backup for that.

ディレクトリ内のすべてのファイルに対してこれを行いたい場合は、次を試すことができます。

$dir_path = '/path/to/your/dir';

$dir = dir($dir_path);
if ($dir !== false) {
    while (($item = $dir->read()) !== false) {
        if ($item == '.' || $item == '..') continue; // skip . and ..
        $path = $dir->path . '/' . $item;
        if (is_dir($path)) continue; // skip directory

        $old_content = file_get_contents($path);

        $html_before = '<html>... something before the old content';
        $html_after = '</html>... something after the old content';

        $result = $html_before . $old_content . $html_after;

        file_put_contents($path, $result); // overwrite the original file!
    }
    $dir->close();
}
于 2012-04-19T10:49:37.480 に答える