fseek
Description
The fseek of Filesystem for PHP seeks on a file pointer.
Syntax
fseek(
resource $stream,
int $offset,
int $whence = SEEK_SET
): intParameters
stream
A file system pointer resource that is typically created using fopen().
offset
The offset.
To move to a position before the end-of-file, you need to pass a negative value in offset and set whence to SEEK_END.
whence
| Constant | Description |
|---|---|
| SEEK_SET | Set position equal to offset bytes. |
| SEEK_CUR | Set position to current location plus offset. |
| SEEK_END | Set position to end-of-file plus offset. |
Return
Returns 0 on success, -1 otherwise.
Examples
1 · handle offset · 0
<?
$filename = $_SERVER['DOCUMENT_ROOT'] . '/assets/txt/1.txt';
$mode = 'r';
$handle = fopen($filename, $mode);
// read data
$data = fgets($handle, 4096);
echo $data . PHP_EOL;
// move to the beginning of the file then offset 0 - same as rewind($handle)
$offset = 0;
fseek($handle, $offset);
// read data
$data = fgets($handle, 4096);
echo $data;
fclose($handle);
hello hello
2 · handle offset · 2
<?
$filename = $_SERVER['DOCUMENT_ROOT'] . '/assets/txt/1.txt';
$mode = 'r';
$handle = fopen($filename, $mode);
// read data
$data = fgets($handle, 4096);
echo $data . PHP_EOL;
// move to the beginning of the file then offset 2
$offset = 2;
fseek($handle, $offset);
// read data
$data = fgets($handle, 4096);
echo $data;
fclose($handle);
hello llo
3 · whence · SEEK_SET
<?
$filename = $_SERVER['DOCUMENT_ROOT'] . '/assets/txt/1.txt';
$mode = 'r';
$handle = fopen($filename, $mode);
// read data
$data = fgets($handle, 4096);
echo $data . PHP_EOL;
// move to the beginning of the file then offset 2
$offset = 2;
$whence = SEEK_SET;
fseek($handle, $offset, $whence);
// read data
$data = fgets($handle, 4096);
echo $data;
fclose($handle);
hello llo
4 · whence · SEEK_CUR
<?
$filename = $_SERVER['DOCUMENT_ROOT'] . '/assets/txt/1.txt';
$mode = 'r';
$handle = fopen($filename, $mode);
// read data
$data = fgets($handle, 3);
echo $data . PHP_EOL;
// move to the current location then offset 2
$offset = 2;
$whence = SEEK_CUR;
fseek($handle, $offset, $whence);
// read data
$data = fgets($handle, 4096);
echo $data;
fclose($handle);
he o
5 · whence · SEEK_END
<?
$filename = $_SERVER['DOCUMENT_ROOT'] . '/assets/txt/1.txt';
$mode = 'r';
$handle = fopen($filename, $mode);
// read data
$data = fgets($handle, 4096);
echo $data . PHP_EOL;
// move to the end of the file then offset -2
$offset = -2;
$whence = SEEK_END;
fseek($handle, $offset, $whence);
// read data
$data = fgets($handle, 4096);
echo $data;
fclose($handle);
hello lo
6 · return
<?
$filename = $_SERVER['DOCUMENT_ROOT'] . '/assets/txt/1.txt';
$mode = 'r';
$handle = fopen($filename, $mode);
$offset = 2;
$return = fseek($handle, $offset);
echo $return;
fclose($handle);
0
Links
Filesystem
- basename
- chgrp
- chmod
- chown
- clearstatcache
- copy
- dirname
- disk_free_space
- disk_total_space
- diskfreespace
- fclose
- feof
- fflush
- fgetc
- fgetcsv
- fgets
- file
- file_exists
- file_get_contents
- file_put_contents
- fileatime
- filectime
- filegroup
- fileinode
- filemtime
- fileowner
- fileperms
- filesize
- filetype
- flock
- fnmatch
- fopen
- fpassthru
- fputcsv
- fputs
- fread
- fscanf
- fstat
- ftell
- ftruncate
- fwrite
- glob
- is_dir
- is_executable
- is_file
- is_link
- is_readable
- is_uploaded_file
- is_writable
- is_writeable
- lchgrp
- lchown
- link
- linkinfo
- lstat
- mkdir
- move_uploaded_file
- pathinfo
- pclose
- popen
- readfile
- readlink
- realpath
- realpath_cache_get
- realpath_cache_size
- rename
- rewind
- rmdir
- set_file_buffer
- stat
- symlink
- tempnam
- tmpfile
- touch
- umask
- unlink