visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: PHP :: editing a text file


By: PHP newbee U.S.A.  Date: 10/06/2003 00:00:00  English  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 English  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 English  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 English  Type : Answer
true. Sorry for the typo. Should read :
"$pathtofile$filename"

Do register to be able to answer

EContact
browser fav
page generated in 834.831950 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page