Languages :: PHP :: editing a text file |
|||
| By: PHP newbee |
Date: 10/06/2003 00:00:00 |
Points: 125 | Status: Answered Quality : Excellent |
|
I need to remove the first line of a text file and save it on the server. Here is what I have so far; $myDirectory = opendir($pathtofile); while ($filename = readdir($myDirectory)) { if ($filename == "$yesterdaysdate.P"){ $myfile = fopen("$pathtofile$filename", "r+"); if (!$myfile) { print "failed to open file for editing"; exit; } } } The first line will always be the same and the subsequent lines will always be different. The file is a tab delimited file for populating a database. Any direction with this will be greatly appreciated. Cheers |
|||
| By: VGR | Date: 10/06/2003 17:11:00 | Type : Comment |
|
| ok. <?php $myDirectory = opendir($pathtofile); while ($filename = readdir($myDirectory)) { if ($filename == "$yesterdaysdate.P"){ $myfile = fopen("$pathtofile$filename", "r+"); if (!$myfile) { print "failed to open file for editing"; exit; } // is read KO else { // let's go // prepare for writing $newfn="you_didn_t_tell.P"; $newfile=fopen($newfn,'w'); // get first line // get subsequent and write them $str=file($myfile); for ($i=1;$i<count($str);$i++) fputs($newfile,$str[$i]); // effectively ignoring first line (index=0) // close everything fclose($myfile); fclose($newfile); } // if read OK } // if filename found } // while not exhausted readdir // NB : don't you have to close/deallocate ($myDirectory) ?!? ?> |
|||
| By: PHP newbee | Date: 10/06/2003 17:23:00 | Type : Comment |
|
| I got the following error with your suggestion; Warning: file() expects parameter 1 to be string, resource given in /home/httpd/docs/adminbroker/getfiles.php on line 67 Cheers |
|||
| By: VGR | Date: 10/06/2003 17:46:00 | Type : Answer |
|
| true. Sorry for the typo. Should read : "$pathtofile$filename" |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








