fseek Fonksiyonu

fseek fonksiyonu, bir dosya içindeki konum göstericisini hareket ettirmenizi sağlar. İlk parametre olarak bir dosya tanıtıcısı, ikinci parametre olarak bayt cinsinden bir ofset ve üçüncü parametre olarak (isteğe bağlı) bir başlangıç noktası alır. Fonksiyon başarı durumunda 0, hata durumunda ise -1 değerini döndürür.

Sözdizimi

phpfseek(resource $handle, int $offset, int $whence = SEEK_SET): int

Örnek

Göstergeyi dosyanın başından itibaren 10. bayta taşıyalım:

php<?php
  $file = fopen('test.txt', 'r');
  fseek($file, 10);
  echo fgets($file);
  fclose($file);
?>

Örnek

Göstergeyi mevcut konumdan itibaren 5 bayt ileri taşıyalım:

php<?php
  $file = fopen('test.txt', 'r');
  fseek($file, 5, SEEK_CUR);
  echo fgets($file);
  fclose($file);
?>

Örnek

Göstergeyi dosyanın sonundan itibaren 5 bayt geriye taşıyalım:

php<?php
  $file = fopen('test.txt', 'r');
  fseek($file, -5, SEEK_END);
  echo fgets($file);
  fclose($file);
?>

Örnek

fseek'in çalışma sonucunu kontrol edelim:

php<?php
  $file = fopen('test.txt', 'r');
  $res = fseek($file, 10);
  echo $res; // Başarı durumunda 0, hata durumunda -1
  fclose($file);
?>

Kodun çıktısı:

php0

Ayrıca Bakınız

  • dosya göstericisini sıfırlayan rewind
    fonksiyonu
  • mevcut konumu döndüren ftell
    fonksiyonu
  • dosyadan okuma yapan fread
    fonksiyonu