douheng8629 2016-02-02 03:20 采纳率: 100%
浏览 41

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

I am new in PHP and still learning about it. This time i want to print some text and save in a file. But i want the file names to be automatically changed.

For example file1.txt, file2.txt, file3.txt, and so on. Here's my code so far:

$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,"
");

}
fclose($of);
  • 写回答

1条回答 默认 最新

  • dongluo8439 2016-02-02 04:01
    关注
    $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,"
    ");
    
        }
    }
    fclose($of);
    

    I hope help you!

    评论

报告相同问题?

悬赏问题

  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法