Home >Backend Development >PHP Tutorial >PHP returns the file pointer read/write location

PHP returns the file pointer read/write location

WBOY
WBOYforward
2024-03-21 20:02:09460browse

This article will explain to you in detail about PHP Returning the read/write position of the file pointer. The editor thinks it is quite practical, so I share it with you as a reference. I hope you will finish reading this article. You can gain something later.

PHP returns the file pointer read/write position

php Provides several functions to return the current read/write position of the file pointer. These functions include:

  • ftell(): Returns the current position of the file pointer, in bytes.
  • fseek(): Move the file pointer to the specified location.
  • rewind(): Move the file pointer to the beginning of the file.
  • feof(): Check whether the file pointer has reached the end of the file.

ftell()

ftell() The function returns the position currently pointed to by the file pointer, in bytes. It is often used to determine how much data a file pointer has read or written into a file.

$file = fopen("myFile.txt", "r");

// Move the file pointer to the end of the file
fseek($file, 0, SEEK_END);

// Get file size
$fileSize = ftell($file);

echo "File size:" . $fileSize . "bytes";

fseek()

fseek() The function moves the file pointer to the specified location. It requires three parameters:

  • $file: The file pointer to be moved.

  • $offset: The offset to move, in bytes.

  • $whence: The reference point of the offset, which can be:

    • SEEK_SET: Start from the beginning of the file.
    • SEEK_CUR: Start from the current position of the file pointer.
    • SEEK_END: Start from the end of the file.
$file = fopen("myFile.txt", "r");

//Move the file pointer to the middle of the file
fseek($file, 50, SEEK_SET);

//Read the data at the current position of the file pointer
$data = fread($file, 10);

echo $data;

rewind()

rewind() The function moves the file pointer to the beginning of the file. It is typically used when a file is being reprocessed or when you want to read the file from scratch.

$file = fopen("myFile.txt", "r");

//Read the first 100 bytes of data from the file
$data1 = fread($file, 100);

// Move the file pointer to the beginning of the file
rewind($file);

// Read the first 100 bytes of data from the file again
$data2 = fread($file, 100);

feof()

feof() Function checks whether the file pointer has reached the end of the file. It returns a Boolean value, true indicating that the end of the file has been reached, false indicating that it has not yet been reached.

$file = fopen("myFile.txt", "r");

while (!feof($file)) {
//Read a line of data from the file
$line = fgets($file);

// Process file lines
}

The above is the detailed content of PHP returns the file pointer read/write location. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lsjlt.com. If there is any infringement, please contact admin@php.cn delete