fseek

(PHP 4, PHP 5, PHP 7)

fseek���ļ�ָ���ж�λ

˵��

fseek ( resource $handle , int $offset [, int $whence = SEEK_SET ] ) : int

���� handle �������ļ����趨�ļ�ָ��λ�á� ��λ�ô��ļ�ͷ��ʼ���ֽ������������� whence ָ����λ�ü��� offset��

In general, it is allowed to seek past the end-of-file; if data is then written, reads in any unwritten region between the end-of-file and the sought position will yield bytes with value 0. However, certain streams may not support this behavior, especially when they have an underlying fixed size storage.

����

handle

�ļ�ϵͳָ�룬�ǵ��͵��� fopen() ������ resource(��Դ)��

offset

ƫ������

Ҫ�ƶ����ļ�β֮ǰ��λ�ã���Ҫ�� offset ����һ����ֵ�������� whence Ϊ SEEK_END��

whence

whence values are:

  • SEEK_SET - �趨λ�õ��� offset �ֽڡ�
  • SEEK_CUR - �趨λ��Ϊ��ǰλ�ü��� offset��
  • SEEK_END - �趨λ��Ϊ�ļ�β���� offset��

����ֵ

�ɹ��򷵻� 0�����򷵻� -1��ע���ƶ��� EOF ֮���λ�ò������

����

Example #1 fseek() ����

<?php

$fp 
fopen('somefile.txt''r');

// read some data
$data fgets($fp4096);

// move back to the beginning of the file
// same as rewind($fp);
fseek($fp0);

?>

ע��

Note:

���ʹ�ø���ģ�ԣ�a �� a+�����κ�д���ļ����ݶ��ᱻ������ȥ�����ļ���λ�ý��ᱻ���ԣ����� fseek() �Ľ����δ���塣

Note:

Not all streams support seeking. For those that do not support seeking, forward seeking from the current position is accomplished by reading and discarding data; other forms of seeking will fail.

�μ�

  • ftell() - �����ļ�ָ���/д��λ��
  • rewind() - �����ļ�ָ���λ��