PHP fgetc()Functions


PHP Filesystem 参考手册Complete PHP Filesystem Reference Manual

Definition and Usage

The fgetc() function returns a single character from an open file.

Syntax

fgetc(file)

Parameters Description
file Required. Specifies the file to check.


Tips and Notes

Note:This function is very slow when processing large files, so it is not used to process large files. If you need to read one character at a time from a large file, use fgets() to read a line of data at a time, and then use fgetc() to process the line data sequentially.


Example 1

<?php
$file = fopen("test2.txt","r");
echo fgetc($file);
fclose($file);
?>

The code above will output:

H


Example 2

Read a file character by character:

<?php
$file = fopen("test2.txt","r");

while (! feof ($file))
{
echo fgetc($file);
}

fclose($file);
?>

The code above will output:

Hello, this is a test file.


PHP Filesystem 参考手册Complete PHP Filesystem Reference Manual Other Extensions