Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP - Direct I/O dio_seek() Function



The PHP Direct I/O dio_seek() function is used to change the position in a file with a resource descriptor.

Syntax

Below is the syntax of the PHP Direct I/O dio_seek() function −

int dio_seek (resource $fd, int $pos, int $whence = SEEK_SET)

Parameters

Below are the parameters of the dio_seek() function −

  • $fd − It is the file descriptor returned by dio_open().

  • $pos − It is the position to seek to.

  • $whence − It is the starting point for the seek operation.

The $whence parameter

The whence parameter can specify how the pos position could be interpreted −

  • SEEK_SET − pos is specified from the beginning of the file.

  • SEEK_CUR − Specify that pos is the number of characters from the current position in the file, and this amount can be positive or negative.

  • SEEK_END − Specifies that pos is the number of characters from the end of a file. A negative value can specify the position within the current file size, and a positive value can specify the position after the end of the file. If we set a position after the current end of the file and write data, we can expand the file with zeros to this position.

Return Value

The dio_seek() function returns 0 on success and -1 on failure.

PHP Version

First introduced in core PHP 4.2.0, the dio_seek() function continues to function easily in PHP 5.1.0.

Example 1

This example demonstrates how to move the file pointer to the beginning of a file and read data using PHP Direct I/O dio_seek() function.

<?php
   // Mention file descriptor here 
   $fd = dio_open('/PHP/PhpProjects/newfile.txt', O_RDONLY);
   dio_seek($fd, 0, SEEK_SET);
   $data = dio_read($fd, 100);
   dio_close($fd);
   echo $data;
?>

Output

The above code will result something like this −

Hello this is a text file.

Example 2

In the below PHP code we will try to use the dio_seek() function and read data from a file and move the file pointer to a specific location. The file pointer is moved to the 50th byte of the file, and 50 bytes are read starting at that point.

<?php
   // Mention file descriptor here
   $fd = dio_open('/PHP/PhpProjects/sample.txt', O_RDONLY);
   dio_seek($fd, 50, SEEK_SET);
   $data = dio_read($fd, 50);
   echo $data;
   dio_close($fd);
?> 

Output

After running the above program, it generates the following output −

.com
Message: Hello, this is a test message.

Example 3

This example will use dio_seek() function so the file pointer can be moved to the end of the file.

<?php
   // Mention file descriptor here
   $fd = dio_open('/PHP/PhpProjects/myfile.txt', O_RDONLY);
   dio_seek($fd, 0, SEEK_END);
   echo "Pointer moved to the end of the file.";
   dio_close($fd);
?> 

Output

This will create the below output −

Pointer moved to the end of the file.

Example 4

This example shows how the file pointer can be relocated relative to its current position with the help of dio_seek() function.

<?php
   // Mention file descriptor here
   $fd = dio_open('/PHP/PhpProjects/newfile.txt', O_RDONLY);
   dio_seek($fd, 10, SEEK_CUR);
   $data = dio_read($fd, 50);
   dio_close($fd);
   echo $data;
?> 

Output

When the above program is executed, it will produce the below output −

 is a text file.
php_function_reference.htm
Advertisements