2016-02-29 318 views
3

我試圖做一個簡單的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的前兩行唯一的工作,但是當它應該閱讀第三行的代碼停止工作。任何建議請。

回答

2
$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

+0

所需要的正是我。謝謝。 – Robert

+0

@Robert ok然後你可以批准答案,如果它已經解決了你的問題 – Ghostman

+0

所以即使你知道這是一個重複的問題! – RiggsFolly

相關問題