?
Solved

PHP / FGETS problem

Posted on 2014-01-28
7
Medium Priority
?
722 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

777 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