Mega Code Archive

 
Categories / Php / File Directory
 

Delete lines from a file

<html> <head> <title>removing lines from a file</title> </head> <body> <?php // this function strips a specific line from a file // if no linenumber is specified, last line is stripped // if a line is stripped, functions returns True else false // // e.g. // cutline('foo.txt'); // strip last line // cutline('foo.txt',1); // strip first line /* its probably a good idea to allow access to this file via a password. otherwise, anyone that knows its in your directory could delete lines of text from the file you specify below! */ function cutline($filename,$line_no=-1) { $strip_return=FALSE; $data=file($filename); $pipe=fopen($filename,'w'); $size=count($data); if($line_no==-1) $skip=$size-1; else $skip=$line_no-1; for($line=0;$line<$size;$line++) if($line!=$skip) fputs($pipe,$data[$line]); else $strip_return=TRUE; return $strip_return; } cutline('foo.txt',6); // deletes line 6 in foo.txt } ?> </body> </html>