【问题标题】:How to Save Results from Foreach into Txt File如何将 Foreach 的结果保存到 Txt 文件中
【发布时间】:2021-05-09 17:50:21
【问题描述】:

我想将特定文件夹中的所有文件写入 .txt 文件。输出工作正常,但我只将一个文件保存到我的 txt 文件中。任何的想法?谢谢!

$fileList = glob('C:\users\John\Documents\*pdf');
foreach($fileList as $filename){
if(is_file($filename)){
echo $filename, '<br>'; 

$save_files = fopen("list.txt", "wb");
fwrite($save_files,$filename);
fclose($save_files);

}   
}  

【问题讨论】:

  • 您正在使用"wb" 打开文件 - 仅供写入;将文件指针放在文件开头并将文件截断为零长度.

标签: php foreach fopen


【解决方案1】:

这是一种方法...

foreach($fileList as $filename){
if(is_file($filename)){
echo $filename, '<br>'; 

$list[] = $filename;
      }   
}

$capture = implode("\r\n",$list);

file_put_contents("list.txt",$capture);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-05
    • 2019-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-21
    • 2020-02-15
    • 1970-01-01
    相关资源
    最近更新 更多