我尝试*.txt
从一个文件夹中读取所有文件,并将每个文件中的所有内容写入另一个txt文件中。但是不知何故,它只将一行写到txt文件中。
我尝试了fwrite()
和file_put_contents()
,但均无济于事。
这是我的代码:
<?php
$dh = opendir('/Applications/XAMPP/xamppfiles/htdocs/test/');
while($file = readdir($dh)) {
$contents = file_get_contents('/Applications/XAMPP/xamppfiles/htdocs/test/' . $file);
$dc = array($contents);
}
file_put_contents('content.txt', $dc);
?>
这应该为您工作:
(在这里,我使用来获取目录中的所有* .txt文件glob()
。此后,我使用foreach循环遍历每个文件,并使用来获取每个文件的内容,file_get_contents()
并使用将该内容放入目标文件中。file_put_contents()
)
<?php
$files = glob("path/*.txt");
$output = "result.txt";
foreach($files as $file) {
$content = file_get_contents($file);
file_put_contents($output, $content, FILE_APPEND);
}
?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句