سؤال

وانا التخمين انها fgets، ولكن أنا لا يمكن العثور على جملة محددة. أحاول أن أقرأ (في سلسلة أنا أفكر هو أسهل) بإضافة السطر الأخير إلى ملف السجل.

هل كانت مفيدة؟

المحلول

وأبسط حل ساذج هو ببساطة:

$file = "/path/to/file";
$data = file($file);
$line = $data[count($data)-1];

وعلى الرغم من هذا سوف تحميل الملف بأكمله في الذاكرة. ربما مشكلة (أو لا). والحل الأفضل هو هذا:

$file = escapeshellarg($file); // for the security concious (should be everyone!)
$line = `tail -n 1 $file`;

نصائح أخرى

وهذا يبدو وكأنه هو ما كنت أبحث عنه:

tekkie.flashbit.net: وظيفة الذيل في PHP

وتنفذ وظيفة يستخدم fseek () مع مؤشر سلبي لنشمر الملف من نهاية المباراة. يمكنك تحديد عدد الأسطر التي تريد يتم إرجاعها.

والتعليمات البرمجية أيضا هو باعتباره جست على جيثب :

// full path to text file
define("TEXT_FILE", "/home/www/default-error.log");
// number of lines to read from the end of file
define("LINES_COUNT", 10);


function read_file($file, $lines) {
    //global $fsize;
    $handle = fopen($file, "r");
    $linecounter = $lines;
    $pos = -2;
    $beginning = false;
    $text = array();
    while ($linecounter > 0) {
        $t = " ";
        while ($t != "\n") {
            if(fseek($handle, $pos, SEEK_END) == -1) {
                $beginning = true; 
                break; 
            }
            $t = fgetc($handle);
            $pos --;
        }
        $linecounter --;
        if ($beginning) {
            rewind($handle);
        }
        $text[$lines-$linecounter-1] = fgets($handle);
        if ($beginning) break;
    }
    fclose ($handle);
    return array_reverse($text);
}

$fsize = round(filesize(TEXT_FILE)/1024/1024,2);

echo "<strong>".TEXT_FILE."</strong>\n\n";
echo "File size is {$fsize} megabytes\n\n";
echo "Last ".LINES_COUNT." lines of the file:\n\n";

$lines = read_file(TEXT_FILE, LINES_COUNT);
foreach ($lines as $line) {
    echo $line;
}
define('YOUR_EOL', "\n");
$fp = fopen('yourfile.txt', 'r');

$pos = -1; $line = ''; $c = '';
do {
    $line = $c . $line;
    fseek($fp, $pos--, SEEK_END);
    $c = fgetc($fp);
} while ($c != YOUR_EOL);

echo $line;

fclose($fp);

وهذا هو أفضل، لأنه لا يتم تحميل الملف كاملا في الذاكرة ...

وتعيين YOUR_EOL لديك خط النهايات الصحيحة، إذا كنت تستخدم خط النهايات نفس خط النهايات الافتراضية لنظام التشغيل حيث يتواجد السيناريو الخاص بك، يمكنك استخدام PHP_EOL ثابت.

function seekLastLine($f) {
    $pos = -2;
    do {
        fseek($f, $pos--, SEEK_END);
        $ch = fgetc($f);
    } while ($ch != "\n");
}

و-2 لأن شار الماضي يمكن \n

واما أن يكون لقراءة الملف في سطرا سطرا وحفظ خط قراءة الماضي للحصول عليه.

وأو إذا كان على يونيكس / لينكس قد تفكر في استخدام ذيل قذيفة القيادة

tail -n 1 filename

وهذا واحد متعود كسر لملف 1 أو 0 خط.

function readlastline($fileName)
{

       $fp = @fopen($fileName, "r");
       $begining = fseek($fp, 0);      
       $pos = -1;
       $t = " ";
       while ($t != "\n") {
             fseek($fp, $pos, SEEK_END);
             if(ftell($fp) == $begining){
              break;
             }
             $t = fgetc($fp);
             $pos = $pos - 1;
       }
       $t = fgets($fp);
       fclose($fp);
       return $t;
}

و... لماذا فقط قراءة السطر الأخير؟

function readLines($fp, $num) {

    $line_count = 0; $line = ''; $pos = -1; $lines = array(); $c = '';

    while($line_count < $num) {
        $line = $c . $line; 
        fseek($fp, $pos--, SEEK_END);
        $c = fgetc($fp);
        if($c == "\n") { $line_count++; $lines[] = $line; $line = ''; $c = ''; }
    }   
    return $lines;
}

$filename = "content.txt";
$fp = @fopen($filename, "r");

print_r(readLines($fp, 2));

fclose($fp);

وunique_stephen، الجواب معيبة. PHP fseek يعود 0 من أجل نجاح و-1 للفشل. تخزين النتيجة في بداية $ (كذا)، ثم في وقت لاحق استخدامه في تصفية ftell () غير صحيحة. إذا كانت سمعتي أعلى كنت قد صوتت لك باستمرار وترك تعليق. هنا هو نسخة معدلة من وظيفة unique_stephen ل.

function readlastline($fileName)
{
    $fp = @fopen($fileName, "r");
    if (fseek($fp, 0) == -1)
        exit('Cannot seek to beginning of the file'); 
    $pos = -1;
    $t = " ";
    while ($t != "\n") {
        if (fseek($fp, $pos, SEEK_END) == -1)
            exit('Cannot seek to the end of the file');
        if (ftell($fp) == 0) {
            break;
        }
        $t = fgetc($fp);
        $pos = $pos - 1;
    }
    $t = fgets($fp);
    fclose($fp);
    return $t;
}

ملحوظة: fseek PHP لا يمكن أن يتمكن من يسعى إلى نهاية ملفات أكبر من PHP_MAX_INT التي وقعت 32BIT وحتى على ثنائيات 64BIT

.
scroll top