2013-04-24 74 views
1

一些信息,我想提出一个控制台查看器为的Minecraft服务器,但是当我到达的阶段,我只需要显示在浏览器中的文本,它不会让我有此错误:显示从页面

致命错误:允许内存大小134217728字节用尽(试图分配36字节)在C:\ xampp \ htdocs \ testingfile.php在第17行上

我猜测它不会显示,因为文件太大?该文件大约是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); 

?> 

回答

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; 
+0

不过,如果我这样做,它不会显示最近的10行:/ 此外,文件的显示方式,顶行是最新的。 – Liton12 2013-04-24 18:39:34

+0

这些命令用于避免致命错误! – auicsc 2013-04-24 18:42:14

+0

好的,我改变了路线。信息正在显示,但它的> 10行,我只想显示。我很抱歉这样的不愉快,我无望在PHP – Liton12 2013-04-24 18:48:07

0

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

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

还有关于如何使用FSEEK一些信息here得到最后几行。

+0

显示现在什么:/ – Liton12 2013-04-24 19:40:29