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 = $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);

归功于 ghostdog74对于此解决方案 link

关于php - 如何从php中的文本文件中删除第一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35696750/

相关文章:

php - 密码验证

php - 代码点火器 : How to make the nested selection for related tables

php - 控制台中出现大量 500 个内部服务器错误(Chrome)

php - mysql 和 php 不支持阿拉伯语

php - REGEX 永远不会与 .htaccess 中的 Origin 匹配

php - 如何使用 OR 和 AND 进行过滤

php - MySQL SUM 只有 1 个字段并且

javascript - 如何在输入文本中使用jquery打印“(quot)而不是”?

php - fatal error : Maximum execution time of 30 seconds exceeded

PHP mySQL numRows() 用法和错误?