如何在PHP中保存文件时迭代文件名?
我是PHP新手,还在学习。这次我想打印一些文本并保存在一个文件中。但是我想自动更改文件名 例如file1.txt、file2.txt、file3.txt等等。 以下是我目前的代码:如何在PHP中保存文件时迭代文件名?,php,filenames,Php,Filenames,我是PHP新手,还在学习。这次我想打印一些文本并保存在一个文件中。但是我想自动更改文件名 例如file1.txt、file2.txt、file3.txt等等。 以下是我目前的代码: $read = file("example.txt",FILE_IGNORE_NEW_LINES); $newFile = "file_1.txt"; $of = fopen($newFile,'w'); foreach($read as $k => $file){ $line = explode("
$read = file("example.txt",FILE_IGNORE_NEW_LINES);
$newFile = "file_1.txt";
$of = fopen($newFile,'w');
foreach($read as $k => $file){
$line = explode(" ",$file);
$line2 = array_slice($line,0,3);
foreach($line2 as $value){
echo $value." ";
fwrite($of,$value." ");
}
echo "<br>";
fwrite($of,"\n");
}
fclose($of);
$read=file(“example.txt”,file\u IGNORE\u NEW\u行);
$newFile=“file_1.txt”;
$of=fopen($newFile,'w');
foreach($k=>$file){
$line=explode(“,$file);
$line2=数组_切片($line,0,3);
foreach($line 2作为$value){
回声$value.“;
fwrite($of,$value.);
}
回声“
”;
fwrite($of,“\n”);
}
fclose(千美元);
$read=file(“example.txt”,file\u IGNORE\u NEW\u行);
$filesToWrite=[“file_1.txt”、“file_2.txt”、“file_3.txt”];
foreach($fileToWrite作为$fileToWrite){
$of=fopen($fileToRead,'w');
foreach($k=>$file){
$line=explode(“,$file);
$line2=数组_切片($line,0,3);
foreach($line 2作为$value){
回声$value.“;
fwrite($of,$value.);
}
回声“
”;
fwrite($of,“\n”);
}
}
fclose(千美元);
我希望能帮助你 $read=file(“example.txt”,file\u IGNORE\u NEW\u行);
$filesToWrite=[“file_1.txt”、“file_2.txt”、“file_3.txt”];
foreach($fileToWrite作为$fileToWrite){
$of=fopen($fileToRead,'w');
foreach($k=>$file){
$line=explode(“,$file);
$line2=数组_切片($line,0,3);
foreach($line 2作为$value){
回声$value.“;
fwrite($of,$value.);
}
回声“
”;
fwrite($of,“\n”);
}
}
fclose(千美元);
我希望能帮助你 首先编写伪代码(即人类可读的描述)。翻译成PHP时,请将此作为注释。首先编写伪代码(即人类可读的描述)。翻译成PHP时,请将此作为注释。谢谢@JoseCarlosRamos,但我需要编写大约400个不同的文件。。如何自动写入?文件名是什么?告诉我如何生成名称文件。谢谢@JoseCarlosRamos,但我需要编写大约400个不同的文件。。如何自动写入?文件名是什么?告诉我如何生成文件名。
$read = file("example.txt",FILE_IGNORE_NEW_LINES);
$filesToWrite = ["file_1.txt", "file_2.txt","file_3.txt"];
foreach($filesToWrite as $fileToWrite){
$of = fopen($fileToRead,'w');
foreach($read as $k => $file){
$line = explode(" ",$file);
$line2 = array_slice($line,0,3);
foreach($line2 as $value){
echo $value." ";
fwrite($of,$value." ");
}
echo "<br>";
fwrite($of,"\n");
}
}
fclose($of);