Solved

How to correctly terminate a text file line so read into PHP?

Posted on 2011-09-28
3
312 Views
Last Modified: 2012-05-12
Hi,

I'm attempting to read a text file using a PHP script and write the data to a MySQL DB.  I'm using PHP version 5.1.4.

I can't get past reading the file.  The code and error messages are shown below.  The image is the text file converted to Hexadecimal.

Note, the end of line characters in hex are: 0D 0A.  Is this correct?  I'm using Ultra Edit to create the test file.  Could my editor be the problem?

How do I fix this?

Thanks!

 


<?php
  $myfile = "C:\\tmp\\text.txt";
  $fh = fopen($myfile, 'r') or die("Can't open file");
  while (!feof($myfile)) {
    $line = fgets($file);
    echo ($line)."<br>";
  }
  fclose($file);
 ?>
  
?>

Warning: feof(): supplied argument is not a valid stream resource in E:\Program Files\nusphere\phped\Projects\ParsMetData&InsertIntoDB.php on line 5

Warning: fgets(): supplied argument is not a valid stream resource in E:\Program Files\nusphere\phped\Projects\ParsMetData&InsertIntoDB.php on line 6

Open in new window

php1.jpg
0
Comment
Question by:IT79637
3 Comments
 
LVL 6

Accepted Solution

by:
neorush earned 300 total points
Comment Utility
Actually your file is fine, the code is incorrect.  You need to use the file handler resource $fh with the file handler functions (feof, fgets, fclose)
<?php
  $myfile = "C:\\tmp\\text.txt";
  $fh = fopen($myfile, 'r') or die("Can't open file");
  while (!feof($fh)) {
    $line = fgets($fh);
    echo ($line)."<br>";
  }
  fclose($fh);
 ?>

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
See http://php.net/manual/en/function.file.php

If you try file() and find blank lines between every valuable line of data, you might want to trim() each line as you read it, then add back the PHP end-of-line character which is always found in the constant PHP_EOL.  It is context-aware.

In case you are reading information from a comma-separated type of file, you might find this function useful.
http://us.php.net/manual/en/function.fgetcsv.php

Best regards, ~Ray
0
 
LVL 1

Author Closing Comment

by:IT79637
Comment Utility
Thanks!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

771 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