如何在PHP中保存文件时迭代文件名?

时间:2016-02-02 03:20:23

标签: php filenames

我是PHP新手,仍然在学习它。这次我想打印一些文本并保存在文件中。但我希望自动更改文件名。

例如file1.txt,file2.txt,file3.txt等。 到目前为止,这是我的代码:

$read = file("example.txt",FILE_IGNORE_NEW_LINES);
$newFile = "file_1.txt";
$of = fopen($newFile,'w');
foreach($read as $k => $file){
    $line = explode(" ",$file);
    $line2 = array_slice($line,0,3);
    foreach($line2 as $value){
        echo $value." ";
        fwrite($of,$value." ");
    }
    echo "<br>";
    fwrite($of,"\n");

}
fclose($of);

1 个答案:

答案 0 :(得分:0)

$read = file("example.txt",FILE_IGNORE_NEW_LINES);

$filesToWrite = ["file_1.txt", "file_2.txt","file_3.txt"];
foreach($filesToWrite as $fileToWrite){
    $of = fopen($fileToRead,'w');
    foreach($read as $k => $file){
        $line = explode(" ",$file);
        $line2 = array_slice($line,0,3);
        foreach($line2 as $value){
            echo $value." ";
            fwrite($of,$value." ");
        }
        echo "<br>";
        fwrite($of,"\n");

    }
}
fclose($of);

我希望能帮到你!