fread(): file is imported all in one line

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MarkAshley
Forum Commoner
Posts: 34
Joined: Fri Nov 18, 2005 1:36 am

fread(): file is imported all in one line

Post by MarkAshley »

Code: Select all

<?php
error_reporting(E_ALL);

$mailFile = "/mail.emlx";
$handle = fopen($mailFile, "r");
if(!$handle)
{
    echo "Couldn't open the data file. Try again later.";
    exit;
}
$mailLines = fread($handle, filesize($mailFile));
fclose($handle);

echo $mailLines;

?>
When this script runs, the contents of the file are echoed to the screen, but it is all one line. If I view the file in a text editor, it is on multiple lines with carriage returns.

I can attach the file if that would help. I imagine the problem is something to do with the encoding of the file, but I don't know how to change the way fopen() opens the file, if I even can?

Thanks
Mark
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

it's because browser's output doesn't recognize/supports carriage returns. Do view source and you'll see it'll be displayed as in original file.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

You could place the data in <pre> </pre> tags.. Or perform nl2br on it...
MarkAshley
Forum Commoner
Posts: 34
Joined: Fri Nov 18, 2005 1:36 am

Post by MarkAshley »

@wtf
If I change echo $mailLines; to echo $mailLines[x]; then it prints just a single character rather than a whole line. Doesn't this suggest that the variable is a string, and mailLines[x] is referring to character x of the string rather than line x of an array?

@timvw
Thanks for the suggestion. I don't actually want to echo the whole thing, I was just doing this for testing. I will be extracting individual strings from the file using ereg(). Because the file is being read as a single line, ereg() returns the whole line when it finds a match.

Any other ideas?

Thanks
Mark
MarkAshley
Forum Commoner
Posts: 34
Joined: Fri Nov 18, 2005 1:36 am

Post by MarkAshley »

I have just been reading this page about text encoding. If I understand correctly, PHP assumes text will be un Unix encoding with single linefeeds. The page also says that the Mac uses single carriage returns. My PHP server is running on MacOS X. Could this be the problem? If I convert the carriage returns to linefeeds, will that work?

The site I linked above lists this command for converting Mac Newline (CR) to Unix Newline (LF): $mailLines =~ s/\r/\n/g;. What would be the result of doing this? Would I then be able to output $mailLines to an array, with each entry in the array containing one of the lines from mailLines?

Thanks
Mark
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

It's as wtf said: Your browser will try to render the data as html, and with html doesn't really care about whitespace and newlines...

Another possibility would be to tell your browser to render it as plain-text with header('content-type: text/plain');
MarkAshley
Forum Commoner
Posts: 34
Joined: Fri Nov 18, 2005 1:36 am

Post by MarkAshley »

Sorry, I don't understand.

I don't actually want to display the variable in the browser. I want the text file to exist in an array, each line of which contains a line of the text file. Then I can address each line as mailLines[x]. Currently, the file is imported as a string in PHP. Echoing, for example, mailLines[3] echoes the fourth character of the file instead of the fourth line.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

file() will sort all your lines into individual array elements,
an alternative would be to use explode() on new lines to generate the same effect.
MarkAshley
Forum Commoner
Posts: 34
Joined: Fri Nov 18, 2005 1:36 am

Post by MarkAshley »

Thanks Jcart! That works perfectly. I guess you're not an Extreme Guru Moderator for nothing :wink:

Mark
Post Reply