0

file.html というファイルがある場合、PHP を使用してこのファイルの 10 個のクローンを作成し、file1....file10 という名前に変更するにはどうすればよいですか?

$filename = 'file.html'
$copyname = 'file2.html'
if ($file = @fopen($copyname, 'x')) {
    // We've successfully created a file, so it's ours.  We'll close
    // our handle.
    if (!@fclose($file)) {
        // There was some problem with our file handle.
        return false;
    }

    // Now we copy over the file we created.
    if (!@copy($filename, $copyname)) {
        // The copy failed, even though we own the file, so we'll clean
        // up by itrying to remove the file and report failure.
        unlink($copyname);
        return false;
    }

    return true;
}
4

2 に答える 2

2

小さなファイルのアプローチ: これにより、ファイルを保存する前に、ファイルの内容に対して何かを行うことができます。

$text = file_get_contents('file.html');
for($i = 0; $i < 100; $i++) {
    file_put_contents('file'.$i.'.html', $data);
}

より大きなファイルのアプローチ: これはファイルを保存する前にファイルの内容にアクセスすることを許可せず、基盤となる OS にコピーを作成するように指示するだけです (Linux の bash コマンドと同等cp file.html file1.html):

for($i = 0; $i < 100; $i++) {
    copy('file.html', 'file'.$i.'.html');
}
于 2012-08-30T11:13:31.060 に答える
0

コードをループで実行するだけです。

$filename = 'file.html'
for($i=1; $i<=10; $i++) {
    $copyname = "file$i.html";
    copy($filename, $copyname);
}

エラーのチェックと処理を自由に追加してください。

于 2012-08-30T11:13:41.143 に答える