Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function delLineFromFile($fileName, $lineNum){
- // check the file exists
- if(!is_writable("chat.txt"))
- {
- // print an error
- print "The file chat.txt is not writable";
- // exit the function
- exit;
- }
- else
- {
- // read the file into an array
- $arr = file("chat.txt");
- }
- // the line to delete is the line number minus 1, because arrays begin at zero
- $lineToDelete = $lineNum-1;
- // check if the line to delete is greater than the length of the file
- if($lineToDelete > sizeof($arr))
- {
- // print an error
- print "You have chosen a line number, <b>[$lineNum]</b>, higher than the length of the file.";
- // exit the function
- exit;
- }
- //remove the line
- unset($arr["$lineToDelete"]);
- // open the file for reading
- if (!$fp = fopen("chat.txt", 'w+'))
- {
- // print an error
- print "Cannot open file (chat.txt)";
- // exit the function
- exit;
- }
- // if $fp is valid
- if($fp)
- {
- // write the array to the file
- foreach($arr as $line) { fwrite($fp,$line); }
- // close the file
- fclose($fp);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement