?
Solved

PHP / FGETS problem

Posted on 2014-01-28
7
Medium Priority
?
735 Views
Last Modified: 2014-01-28
I am currently using FGETS to read through a file, but I seem to have an odd problem - its missing lines? - it looks like for every 1 line it displays, it skips the next 2 lines

The input file is an excel CSV (basically plain text) - but i'm wondering if that could be my issue.

The CSV data has only one column, so has no commas and looks like this:

DataLine1 has 50ish chars
Dataline2 has 50ish chars
Dataline3 has 50ish chars
Dataline4 has 50ish chars
Dataline5 has 80ish chars
Dataline6 has 80ish chars
Dataline7 has 80ish chars
Dataline8 has 80ish chars


The code I am using to read it is as follows:

$file = fopen("files/tempfile.csv", "r") or exit("Unable to open file for reading!");
		 while(!feof($file))
		   {
			   			echo fgets($file) . "</br>";
}
fclose($file);

Open in new window


The output I get is like this:

DataLine1 has 50ish chars
Dataline4 has 50ish chars
Dataline7 has 80ish chars

Any ideas how I can get it to read the file properly, without missing most of the data?

Thanks
0
Comment
Question by:Bergstr
  • 3
  • 2
  • 2
7 Comments
 
LVL 10

Expert Comment

by:ienaxxx
ID: 39814713
Try fgets($file, 4096).
0
 

Author Comment

by:Bergstr
ID: 39814729
Nope, still have the same problem...
0
 
LVL 10

Assisted Solution

by:ienaxxx
ienaxxx earned 800 total points
ID: 39814734
OK then, try this:

ini_set('auto_detect_line_endings', true);
file = fopen("files/tempfile.csv", "r") or exit("Unable to open file for reading!");
		 while(!feof($file))
		   {
			   			echo fgets($file) . "</br>";
}
fclose($file);

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:Amar Bardoliwala
ID: 39814740
Hello Bergstr,

can you please give your original file that you are trying ?

it should help to resolve the problem.

Thank you.

Amar Bardoliwala
0
 

Author Comment

by:Bergstr
ID: 39814780
Think I found my problem - I didn't put the additional code before or after that section in my post, but it appears to be relevant (sorry!)

I'm trying to read from the old file, validate that data (and change it if needs be) then write the data to another file - and that seems to be my problem!


$file = fopen("files/tempfile.csv", "r") or exit("Unable to open file for reading");
$newfile = fopen("files/newfile.csv", 'w') or exit("Unable to open file for writing");
 //Output a line of the file until the end is reached
 while(!feof($file))
   {
   echo fgets($file). "<br>";
   fwrite($newfile, fgets($file));
   }
 fclose($file);
 
}

Open in new window


If I remove the fwrite line, it works fine - soon as I add the one line lines start disappearing!

I guess the new question is, how do I read one file while writing that data back to another, without losing data?

Sorry for being a pain, and thanks for the responses!
0
 
LVL 11

Accepted Solution

by:
Amar Bardoliwala earned 1200 total points
ID: 39814804
Hello Bergstr,

Please store your current line in some variable first and use that like following

while(!feof($file))
   {
        $line =  fgets($file);
   
        echo $line. "<br />";

        fwrite($newfile, $line);
   }

Open in new window


currently it seems you are using fgets directly to print and it seems that it might echo first line and add second line in file..
so I can say that in while loop you are fetching 2 lines at a time.
because each time to write fgets it will fetch 1 line from file.

Hope this will help you.

Thank you.

Amar Bardoliwala
0
 

Author Comment

by:Bergstr
ID: 39814816
That seems to have worked, thanks for your help!

I'll accept the above as the solution but also share the points with ienaxxx s they did try and help when my original question didn't provide full details!

Thanks both!
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month15 days, 20 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question