1

I am making a console viewer for a Minecraft server, but when I get to the stage where I only need to display the text in a browser, It wont let me with this error:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 36 bytes) in C:\xampp\htdocs\testingfile.php on line 17

I'm Guessing that it wont display because the file is too big? The file is about 4.5MB. I want to display the 10 most recent lines from the file.

Here is my code:

    <?php

// define some variables
$local_file = 'C:\Users\Oscar\Desktop\worked.txt';
$server_file = 'NN7776801/server.log';
$ftp_server="Lol.Im.Not.Thick";
$ftp_user_name="Jesus";
$ftp_user_pass="ReallyLongPassWordThatYouWontGuessLolGoodLuckMateGuessingThisPass";

$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// try to download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
    $contents = file($local_file); 
    $string = implode($contents); 
    echo $string;


    for ($i = 0; $i < 6; $i++) {
    echo $local_file[$i] . "\n";
}
}
else {
    echo "There was a problem\n";
}
// close the connection
ftp_close($conn_id);

?>
Liton12
  • 11
  • 4

2 Answers2

0

Increase the following in php.ini variables so that your page execution will not stop:

max_input_time
memory_limit
max_execution_time

For getting 10 lines you could try something like:

$filearray = file("filename");
$lastfifteenlines = array_slice($filearray,-15);

Or using a function:

function read_last_lines($fp, $num)
{
    $idx   = 0;

    $lines = array();
    while(($line = fgets($fp)))
    {
        $lines[$idx] = $line;
        $idx = ($idx + 1) % $num;
    }

    $p1 = array_slice($lines,    $idx);
    $p2 = array_slice($lines, 0, $idx);
    $ordered_lines = array_merge($p1, $p2);

    return $ordered_lines;
}

// Open the file and read the last 15 lines
$fp    = fopen('C:\Users\Oscar\Desktop\worked.txt';', 'r');
$lines = read_last_lines($fp, 10);
fclose($fp);

// Output array 
 echo '<pre>'.print_r($my_array).'</pre>';

To print the content add this:

$withlines= implode ("<br>\n",$my_array); //Change $my_array with the name you used!
echo $withlines;
auicsc
  • 297
  • 1
  • 3
  • 14
  • Still, if i were to do that, It wouldn't display the most recent 10 lines :/ Also, the way the file is displayed, the top lines are the most recent. – Liton12 Apr 24 '13 at 18:39
  • Okay, Ive changed the lines. The Info is displaying, but its > 10 Lines, which I'd like to display only. I'm sorry for such noobiness, i'm hopeless at PHP – Liton12 Apr 24 '13 at 18:48
  • What A Lovely Guy! It worked! :D. But the lines are mumbo-jumbo, how do i display them neatly one line-per-line – Liton12 Apr 24 '13 at 19:49
  • Didn't Work :/, it also says stuff like Array => [1]=> text here [2]=> text here etc... – Liton12 Apr 26 '13 at 14:55
  • could you do something like : var_dump($my_array) and post the results? – auicsc Apr 26 '13 at 18:33
  • Nope, it just displays this: http://intogamingserver.co.uk/stackoverflow.png Like i Said, the lines are all scrambled :/ – Liton12 Apr 26 '13 at 18:58
  • You Must Be Jesus. Thank You Very Much Mate :-) It Worked! – Liton12 Apr 27 '13 at 08:15
  • How do i do dat? :P I'm new at dis EDIT: Don't matter, did it :D – Liton12 Apr 28 '13 at 15:17
0

If you only need the last 10 lines, you can use tail

$lines = `tail -n 10 $local_file`;

There's also some information here on how to use fseek to get the last few lines.

Community
  • 1
  • 1
aynber
  • 22,380
  • 8
  • 50
  • 63