fseek 


php128 apg

SETS the file position indicator.





If you have opened the file in append 'a' or 'a+' mode, any data you write to the file will always be appended, regardless of the file position, and the result of calling fseek will be undefined.

Not every STREAM supports this function.

For those who don't support it, the direct search from the current position is performed by reading and discarding data; other forms of search will fail.

This function upon success, returns 0; otherwise, returns -1.



<?php

int fseek 
( res $stream ,
                
int $offset , 
                
int $whence = SEEK_SET )


where,

$stream = A file system pointer

$offset 
= The offset

$whence 
= To set the position
                 
(SEE the below table)

?>

 $stream 


A file system pointer typically created by fopen.



  $offset   


The offset.



  $whence   


The position to be established using the values of the below table:



WHENCE SEQUENCE VALUES MEANING
SEEK_SET 0 Set position equal to $offset bytes
SEEK_CUR 1 Set position to current location plus $offset
SEEK_END 2 Set position to end-of-file plus $offset
ed48

  1 EXERCISE   

<?php

$file01 
= 'file://' . __DIR__ . '/temp/blahblahblah.txt';

echo 
$file01 . '<br><br>';

$fp01 = fopen($file01, 'r');

$data01 = fread($fp01, 4096);

$seek01 = fseek($fp01 , SEEK_SET);

if (
$seek01 == 0)
{
    echo 
'Success...';
    
fclose($fp01);
}
else
{
    echo 
'Fail...';
}

?>

  2 EXERCISE   

<?php

$file02 
= __DIR__ . '/temp/sw7.jpg';

echo 
$file02 . '<br><br>';

$fp02 = fopen($file02, 'r');

$data02 = fread($fp02, 8192);

$seek02 = fseek($fp02 , 1);
// SEEK_CUR

if ($seek02 == 0)
{
    echo 
'Success...';
    
fclose($fp02);
}
else
{
    echo 
'Fail...';
}

?>