Solved

PHP / FGETS problem

Posted on 2014-01-28
7
681 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 200 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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 300 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now