Thursday, 27 July 2017

Membaca Beberapa Baris Terakhir dari Sebuah File

Terkadang kita perlu membaca beberapa bari terakhir dari sebuah file, misal untuk membaca file log. Memang, di Linux kita dapat menggunakan perintah tail, tapi solusi ini tidak berjalan di semua sistem operasi.

Alhamdulillah, Om Lorenzo sudah memberikan solusi untuk ini, banyak cara yang memungkinkan [1], tapi Om Lorenzo sudah memastikan bawa cara dibawah ini adalah cara yang terbaik, jangan lupa, setiap baris pada file log, harus diakhir dengan new line character \n :

<?php
/**
* Slightly modified version of http://www.geekality.net/2011/05/28/php-tail-tackling-large-files/
* @author Torleif Berger, Lorenzo Stanco
* @link http://stackoverflow.com/a/15025877/995958
* @license http://creativecommons.org/licenses/by/3.0/
*/
function tailCustom($filepath, $lines = 1, $adaptive = true) {
// Open file
$f = @fopen($filepath, "rb");
if ($f === false) return false;
// Sets buffer size, according to the number of lines to retrieve.
// This gives a performance boost when reading a few lines from the file.
if (!$adaptive) $buffer = 4096;
else $buffer = ($lines < 2 ? 64 : ($lines < 10 ? 512 : 4096));
// Jump to last character
fseek($f, -1, SEEK_END);
// Read it and adjust line number if necessary
// (Otherwise the result would be wrong if file doesn't end with a blank line)
if (fread($f, 1) != "\n") $lines -= 1;
// Start reading
$output = '';
$chunk = '';
// While we would like more
while (ftell($f) > 0 && $lines >= 0) {
// Figure out how far back we should jump
$seek = min(ftell($f), $buffer);
// Do the jump (backwards, relative to where we are)
fseek($f, -$seek, SEEK_CUR);
// Read a chunk and prepend it to our output
$output = ($chunk = fread($f, $seek)) . $output;
// Jump back to where we started reading
fseek($f, -mb_strlen($chunk, '8bit'), SEEK_CUR);
// Decrease our line counter
$lines -= substr_count($chunk, "\n");
}
// While we have too many lines
// (Because of buffer size we might have read too many)
while ($lines++ < 0) {
// Find first newline and remove all text before that
$output = substr($output, strpos($output, "\n") + 1);
}
// Close file and return
fclose($f);
return trim($output);
}
?>
view raw gistfile1.php hosted with ❤ by GitHub

Referensi


  1. What is the best way in PHP to read last lines from a file?, https://stackoverflow.com/questions/15025875/what-is-the-best-way-in-php-to-read-last-lines-from-a-file/15025877#15025877

Wednesday, 26 July 2017

PhpSpreadsheet : Protected Cells

Status : Draft

Berikut hanya potongan-potongan kode saja :

//style cell yang dapat diedit
$editable_cell_style = array(
 'fill' => array(
  'type' => PHPExcel_Style_Fill::FILL_SOLID,
  'color' => array('rgb' => '66ff99')
  )
);

//protect all cell by default
$sheet->getProtection()->setPassword('amanah');
$sheet->getProtection()->setSheet(true);

$sheet->getStyle('C6')->getProtection()->setLocked(PHPExcel_Style_Protection::PROTECTION_UNPROTECTED);
$sheet->getStyle('C6')->applyFromArray($editable_cell_style);

//sembunyikan kolom A
$sheet->getColumnDimension("A")->setVisible(false);