2013-04-24 120 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