★ wanayoo — archive 1999 http://uk.php.net/manual/cs/function.fseek.phpNouvelle recherche | Portail wanayoo
PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<fscanffstat>
view the version of this page
Last updated: Wed, 20 Aug 2003

fseek

(PHP 3, PHP 4 )

fseek -- Posouvá ukazatel v souboru

Popis

int fseek ( int fp, int offset [, int whence])

Nastaví ukazatel pozice v souboru specifikované pomocí deskriptoru fp. Nová pozice, měřená počtem bytů od začátku souboru, je získána přičtením hodnoty offset k pozici specifikované pomocí whence, jehož hodnota je definovanána:

SEEK_SET - Nastav na pozici rovnu offset bytů.
SEEK_CUR - Nastav pozici na současnou plus počet bytů v offset.
SEEK_END - Nastav na konec souboru plus offset bytů.

Pokud není parametr whence specifikován, použije se SEEK_SET.

Při úspěchu vrací 0, jinak -1. Pozn.: přesun za konec souboru není považován za chybu.

Nelze použít na deskriptor souboru vrácený funkcí fopen(), jestliže byl použit formát "http://" nebo "ftp://".

Poznámka: Argument whence byl přidán po verzi PHP 4.0 RC1.

Viz také ftell() a rewind().



add a note add a note User Contributed Notes
fseek
jim at lfchosting dot com
04-Nov-2003 11:03
Here is a function that returns the last line of a file.  This should be quicker than reading the whole file till you get to the last line.  If you want to speed it up a bit, you can set the $pos = some number that is just greater than the line length.  The files I was dealing with were various lengths, so this worked for me.

function readlastline()
{
        $fp = @fopen("/dosmnt/LOGFILE.DAT", "r");
        $pos = -1;
        $t = " ";
        while ($t != "\n") {
              fseek($fp, $pos, SEEK_END);
              $t = fgetc($fp);
              $pos = $pos - 1;
        }
        $t = fgets($fp);
        fclose($fp);
        return $t;
}
aspyrine at hotmail dot com
11-Mar-2003 12:51
If you want to go to the end of a socket stream with fseek() you'll get the following error :
"stream does not support seeking"

feof() wont work eiver in a stream (ie. smtp)

You can move the pointer to the end with this command :
while(fgetc($fp)) {}
...so easy :-)
16-Sep-2002 06:25
Don't use filesize() on files that may be accessed and updated by parallel processes or threads (as the filesize() return value is maintained in a cache).
Instead lock the opened file and use fseek($fp,0,SEEK_END) and ftell($fp) to get the actual filesize if you need to perform a fread() call to read the whole file...
24-Aug-2002 10:12
The following call moves to the end of file (i.e. just after the last byte of the file):

    fseek($fp, 0, SEEK_END);

It can be used to tell the size of an opened file when the file name is unknown and can't be used with the filesize() function:

    fseek($fp, 0, SEEK_END);
    $filesize = ftell($fp);

The following call moves to the begining of file:

    fseek($fp, 0, SEEK_SET);

It is equivalent to:

    rewind($fp);
dan at daniellampert dot com
01-Jan-2002 07:54
For all first-time users of the fseek() function, remember these three things:

1. to use a programming expression, fseek() is "base 0", so to prepare the file for writing at character 1, you'd say fseek($fp,0); and to prepare the file for writing at character $num, you'd say fseek($fp,($num-1));

2. here's the formula for accessing fixed-length records in a file (you need to seek the position of the end of the previous record):

  /* assumes the desired record number is in $rec_num */
  /* assumes the record length is in $rec_len */
  $pos = ( ($rec_num-1) * $rec_len );
  fseek($fp,$pos);

3. if you're using fseek() to write data to a file, remember to open the file in "r+" mode, example:

  $fp=fopen($filename,"r+");

Don't open the file in mode "a" (for append), because it puts the file pointer at the end of the file and doesn't let you fseek earlier positions in the file (it didn't for me!). Also, don't open the file in mode "w" -- although this puts you at the beginning of the file -- because it wipes out all data in the file.

Hope this helps.

<fscanffstat>
 Last updated: Wed, 20 Aug 2003
show source | credits | sitemap | mirror sites 
Copyright © 2001-2003 The PHP Group
All rights reserved.
This mirror generously provided by: Kewlio.net Limited
Last updated: Thu Nov 6 09:12:56 2003 GMT