我尝试从文件夹中读取所有的*.txt文件,并将每个文件的所有内容写入另一个txt文件中。但是不知怎么的,它只写入了一行文本。
我已经尝试使用fwrite()和file_put_contents()函数,但都没有成功。
以下是我的代码:
我已经尝试使用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);
?>
file_put_contents()
中设置了FILE_APPEND
标志,以便它实际上将内容附加到文件中。 - Rizier123glob()
中的路径是正确的!在 foreach 循环之前执行print_r($files);
以查看有哪些文件。 - Rizier123