PHP生成TXT文件

来源:百度知道 编辑:UC知道 时间:2024/06/24 04:50:29
<?php
if ($handle = opendir('./txt/')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo "$file";
}
}
closedir($handle);
}
>?

将上面的代码的执行结果写入到dir.txt这个文件中,如何实现?

把这页保存起来,比如http://localhost/a.php
然后再另外一页写
<?php
$content = file_get_contents("http://localhost/a.php");//得到文件执行的结果
$of = fopen('dir.txt','w');//创建并打开dir.txt
if($of){
fwrite($of,$content);//把执行文件的结果写入txt文件
}
fclose($of);
?>

<?php
$string="";
if ($handle = opendir('./txt/')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$string.=$file."\n\r";
}
}
closedir($handle);
}

writedir($string);
>?
大概就这样吧,writedir是自己定义的函数,就是把字符写到文件里去,怎么写文件,查查手册吧,我这里没手册,所有写不了具体的方法

网上查一下
fwrite

file_put_contents('dir.txt',$file);