如何在php中删除文本文件的第一行
我尝试制作一个简单的php来读取和删除文本文件的第一行 到目前为止,我得到了密码:如何在php中删除文本文件的第一行,php,Php,我尝试制作一个简单的php来读取和删除文本文件的第一行 到目前为止,我得到了密码: <?php $myfile = fopen("text.txt", "r") or die("Unable to open file!"); $ch=1; while(!feof($myfile)) { $dataline= fgets($myfile); $listes = explode("\n",file_get_contents("text.txt")); $poc = $li
<?php
$myfile = fopen("text.txt", "r") or die("Unable to open file!");
$ch=1;
while(!feof($myfile)) {
$dataline= fgets($myfile);
$listes = explode("\n",file_get_contents("text.txt"));
$poc = $listes[0];
if($ch == 2){
$gol = str_replace(' ', ' ', $dataline)."\n";
$fh = fopen("newtext.txt",'a');
fputs($fh, $gol);
fclose($fh);
chmod("newtext.txt", 0777);
}
$ch = 2;
}
unlink("text.txt");
copy("newtext.txt", "text.txt");
chmod("text.txt", 0777);
unlink("newtext.txt");
fclose($myfile);
echo $poc;
flush();
?>
该代码仅适用于text.txt中的前两行,但当它应该读取第三行时,该代码将停止工作。有什么建议吗
$handle = fopen("file", "r");
$first = fgets($handle,2048); #get first line.
$outfile="temp";
$o = fopen($outfile,"w");
while (!feof($handle)) {
$buffer = fgets($handle,2048);
fwrite($o,$buffer);
}
fclose($handle);
fclose($o);
rename($outfile,$file);
此解决方案的积分为正是我所需要的。谢谢你。@Robert好的,如果答案解决了你的问题,那么你就可以批准它,所以即使你知道这是一个重复的问题!