2

I am trying to read (and echo) everything of a .txt-File.

This is my code:

$handle = @fopen("item_sets.txt", "r");

while (!feof($handle))
{
    $buffer = fgets($handle, 4096);
    $trimmed = trim($buffer);
    echo $trimmed;
}

This is my "item_sets.txt": http://pastebin.com/sxapZGuW

But it doesn't echo everything (and changing how much it shows depending on if and how many characters i echo after it). var_dump() shows me that the last string is never finished printing out. That looks like this:

" string(45) ""[cu_well_tra. But if I put an

echo "whateverthisisjustarandomstringwithseveralcharacters";,

my last output lines look like this:

" string(45) ""[cu_well_traveled_ak47]weapon_ak47" "1" " string(5) "} "

Basically my code isn't printing/echoing all of what it should or at least not showing it.

Thanks in advance :)

3
  • 1
    why not using print file_get_contents() here? Commented Dec 22, 2015 at 23:05
  • @ob_start still isn't printing everything Commented Dec 22, 2015 at 23:09
  • 1
    There is nothing wrong with your code - it reads all the lines and outputs them. Except trim 'eats' all the end of line markers so when you echo you need to use PHP_EOL. i,e. echo $trimmed, PHP_EOL;. Otherwise you get one long line of output. Commented Dec 23, 2015 at 1:46

1 Answer 1

3

Thats because your test for EOF is before you output your last read

Try this with the test for EOF as part of the reading process

<?php
$line_count = 0;

$handle = fopen("item_sets.txt", "r");

if ($handle) {

    while (($buffer = fgets($handle, 4096)) !== false) {
        $trimmed = trim($buffer);
        echo $trimmed;
        $line_count++;
    }
} else {
    echo 'Unexpected error opening file';
}
fclose($handle);

echo PHP_EOL.PHP_EOL.PHP_EOL.'Lines read from file = ' . $line_count;
?>

Also I removed the @ infront of the fopen its bad practice to ignore errors, and much better practice to look for them and deal with them.

I copied your data into a file called tst.txt and ran this exact code

<?php
$handle = fopen('tst.txt', 'r');

if ($handle) {

    while (($buffer = fgets($handle, 4096)) !== false) {
        $trimmed = trim($buffer);
        echo $trimmed;
    }
} else {
    echo 'Unexpected error opening file';
}
fclose($handle);

And it generated this output ( just a small portion shown here )

"item_sets"{"set_community_3"{"name"            "#CSGO_set_community_3""set_description"                "#CSGO_set_community_3_desc""is_collection"

And the last output is

[aa_fade_revolver]weapon_revolver"          "1"

Which is the last entry in the data file

Sign up to request clarification or add additional context in comments.

16 Comments

This is also not printing everything :/ Could this just be an output problem ?
Add the debug line, to see if it is actually going round that loop at all
Well ... now it's not showing anything anymore, it's literally not even showing my html document I had around besides the navigation bar
And have you looked in your php error log for any hints. Also remember I can only see what you have shown me, I see no html therefore I treated this as a stand alone piece of code
Last log of the error log was when I tried to print it out with file_get_contents()
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.