리눅스 Tail 명령어처럼 PHP 에서 라인제한 출력
리눅스 tail 명령어처럼 php에서 특정파일을 웹으로 출력하는 소스입니다.
저도 어디서 퍼온건데.. 기억이 나질 안네요..ㅜㅜ
(원작자분 댓글이나 메일 주시면 출처 표시하겠습니다..)
<?php ⁄⁄ full path to text file define("TEXT_FILE", "⁄var⁄log⁄messages"); ⁄⁄ number of lines to read from the end of file define("LINES_COUNT", 100); function file_tail($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><br>"; echo "File size is {$fsize} megabytes<br>"; echo "Last ".LINES_COUNT." lines of the file:<br>"; $lines = file_tail(TEXT_FILE, LINES_COUNT); foreach ($lines as $line) { echo $line; echo '<br>'; } ?> ▽도움이 되셨다면 아래의 손가락 과 광고배너 한번 클릭해주는 센스는 매우 감사ㅠ_ㅠ▽
'프로그램 언어 > PHP' 카테고리의 다른 글
PHP 문자열 관련 함수들 (0) | 2013.01.30 |
---|---|
PHP 한글문자열 자르기 Function (0) | 2012.12.17 |