php批量生成文件为何只生成了最后一个?

来源:百度知道 编辑:UC知道 时间:2024/06/06 02:12:28
list.txt内容:
aaa
bbb
ccc

下面是代码:
<?
$a=file("list.txt");
foreach ($a as $b) {
$filename=$b.".htm";
$content=$b;
if(!file_exists($filename)){
$file = fopen($filename,"w");
fwrite($file,$content);
fclose($file);
}
}
?>

为何最后只生成了ccc.htm?

$filename = trim($b).".htm";
$content = trim($b);
文件读取后每行后含有回车换行符,去掉后就没问题了
<?
$a=file("list.txt");
foreach ($a as $b) {
$filename = trim($b).".htm";
$content = trim($b);
$handle = fopen($filename,"w"); //write open file
if (!is_writable ($filename)){
echo 'warning:'.$filename.'path not writable!';
}
if (!fwrite ($handle,$content)){
echo 'warning:'.$filename.'path not writable!';
}
//replace contents
fclose($handle);
}
?>

没有循环吧

foreach ($a as $b) {

修改
foreach ($a as $b =>$v) {

<?
$a=file("list.txt");

foreach ($a as $key=>$b)
{
echo $b;
$fname=trim($b).".htm";
//文件每行都有一个回车字符,不可见的。
//你吧下面的封了,运行看看源码,输出的没有BR也换行了?
$content="测试——".$b;
if(!file_exists($fname))
{