PHP fseek() Function

In this chapter you will learn:

  1. Definition for PHP fseek() Function
  2. Syntax for PHP fseek() Function
  3. Parameter for PHP fseek() Function
  4. Return for PHP fseek() Function
  5. Example - fseek() moves a file handle pointer to an arbitrary position

Definition

The fseek() function seeks in an open file, by moving the file pointer from its current position to a new position, forward or backward, specified by the number of bytes.

Syntax

PHP fseek() Function has the following syntax.

fseek(file,offset,whence)

Parameter

ParameterIs RequiredDescription
fileRequired.Open file to seek in
offsetRequired.New position (measured in bytes from the beginning of the file)
whenceOptional.Option

Possible values for whence:

  • SEEK_SET - Set position equal to offset. Default
  • SEEK_CUR - Set position to current location plus offset
  • SEEK_END - Set position to EOF plus offset to move to a position before EOF, the offset must be a negative value

Return

This function returns 0 on success, or -1 on failure. Seeking past EOF will not generate an error.

Example

fseek() moves a file handle pointer to an arbitrary position.


<?PHP//ja  va 2 s.com
      $filename = "c:/abc/test.txt";
      $handle = fopen($filename, "w+");
      fwrite($handle, "java2s.com\n");
      rewind($handle);
      fseek($handle, 1);
      fwrite($handle, "o");
      fseek($handle, 2, SEEK_CUR);
      fwrite($handle, "e");
      fclose($handle);
?>

The first byte of a file is byte 0, and you count upward from there. The second byte is at index 1, the third at index 2, etc.

Find the current position by using ftell().

Next chapter...

What you will learn in the next chapter:

  1. Definition for PHP fstat() Function
  2. Syntax for PHP fstat() Function
  3. Parameter for PHP fstat() Function
  4. Return for PHP fstat() Function
  5. Note on PHP fstat() Function
  6. Example - returns information about an open file
Home » PHP Tutorial » PHP File Functions
PHP basename() function
PHP chdir() function
PHP chgrp() Function
PHP chmod() Function
PHP chown() Function
PHP chroot() Function
PHP clearstatcache() Function
PHP closedir() Function
PHP copy() Function
PHP delete() function
PHP dir() Function
PHP dirname() Function
PHP disk_free_space() Function
PHP disk_total_space() Function
PHP diskfreespace() Function
PHP fclose() Function
PHP feof() Function
PHP fflush() Function
PHP fgetc() Function
PHP fgets() Function
PHP fgetss() Function
PHP file() Function
PHP file_exists() Function
PHP file_get_contents() Function
PHP file_put_contents() Function
PHP fileatime() Function
PHP filectime() Function
PHP filegroup() Function
PHP fileinode() Function
PHP filemtime() Function
PHP fileowner() Function
PHP fileperms() Function
PHP filesize() Function
PHP filetype() Function
PHP flock() Function
PHP fnmatch() Function
PHP fopen() Function
PHP fpassthru() Function
PHP fprintf() Function
PHP fputcsv() Function
PHP fputs() Function
PHP fread() Function
PHP fscanf() Function
PHP fseek() Function
PHP fstat() Function
PHP ftell() Function
PHP ftruncate() Function
PHP fwrite() Function
PHP getcwd() function
PHP glob() Function
PHP is_dir() Function
PHP is_executable() Function
PHP is_file() Function
PHP is_link() Function
PHP is_readable() Function
PHP is_uploaded_file() Function
PHP is_writable() Function
PHP link() Function
PHP linkinfo() Function
PHP lstat() Function
PHP md5_file() Function
PHP mkdir() Function
PHP move_uploaded_file() Function
PHP opendir() Function
PHP parse_ini_file() Function
PHP pathinfo() Function
PHP pclose() Function
PHP popen() Function
PHP readdir() Function
PHP readfile() Function
PHP readlink() Function
PHP realpath() Function
PHP rename() Function
PHP rewind() Function
PHP rewinddir() Function
PHP rmdir() Function
PHP scandir() Function
PHP set_file_buffer() Function
PHP sha1_file() Function
PHP stat() Function
PHP symlink() Function
PHP tempnam() Function
PHP tmpfile() Function
PHP touch() Function
PHP umask() Function
PHP unlink() Function
PHP vfprintf() Function