显示页面中的一些信息

时间:2013-04-24 18:35:11

标签: php minecraft

我正在为Minecraft服务器制作一个控制台查看器,但当我进入我只需要在浏览器中显示文本的阶段时,它不会让我犯这个错误:

致命错误:第17行的C:\ xampp \ htdocs \ testingfile.php中允许的内存大小为134217728个字节(试图分配36个字节)

我猜它不会显示因为文件太大了?该文件大约是4.5MB。 我想显示文件中最近的10行。

这是我的代码:

    <?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);

?>

2 个答案:

答案 0 :(得分:0)

增加php.ini变量中的以下内容,以便您的页面执行不会停止:

max_input_time
memory_limit
max_execution_time

要获得10行,您可以尝试以下内容:

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

或使用功能:

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>';

要打印内容,请添加以下内容:

$withlines= implode ("<br>\n",$my_array); //Change $my_array with the name you used!
echo $withlines;

答案 1 :(得分:0)

如果你只需要最后10行,你可以使用尾

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

还有一些关于如何使用fseek获取最后几行的信息here

相关问题