Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP / FGETS problem

Posted on 2014-01-28
7
Medium Priority
?
729 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

618 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