PHP returns the file pointer read/write location

WBOY
Release: 2024-03-21 20:04:02
forward
424 people have browsed it

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";
Copy after login

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;
Copy after login

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);
Copy after login

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
}
Copy after login

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!

source:lsjlt.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!