It returns the current position of the file pointer
Example
<?php
$fp = fopen(“test.txt”, “r”);
$data = fgets($fp, 5);
echo ftell($fp);
?>
Output
4
It returns the current position of the file pointer
Example
<?php
$fp = fopen(“test.txt”, “r”);
$data = fgets($fp, 5);
echo ftell($fp);
?>
Output
4
It returns information about a file using an open file pointer
Example
<?php
$fp = fopen(‘test.txt’, ‘r’);
$stats = fstat($fp);
echo “<pre>”;
print_r($stats);
?>
Output
Array
(
[0] => 2049
[1] => 673768
[2] => 33279
[3] => 1
[4] => 1000
[5] => 1000
[6] => 0
[7] => 8
[8] => 1336732795
[9] => 1336732794
[10] => 1336732794
[11] => 4096
[12] => 8
[dev] => 2049
[ino] => 673768
[mode] => 33279
[nlink] => 1
[uid] => 1000
[gid] => 1000
[rdev] => 0
[size] => 8
[atime] => 1336732795
[mtime] => 1336732794
[ctime] => 1336732794
[blksize] => 4096
[blocks] => 8
)
It seeks on a file pointer
Example
<?php
$fp = fopen(‘test.txt’, ‘r’);
$data = fgets($fp, 2048);
fseek($fp, 0);
?>
It parses input from a file according to a format
Example
<?php
$fp = fopen(“test.txt”, “r”);
while ($data = fscanf($fp, “%tn%sn”)) {
list ($name, $profession, $countrycode) = $data;
}
fclose($fp);
?>
It can be used to read the content from a file
Example
<?php
$fp = fopen(‘test.txt’, ‘r+’);
echo fread($fp,filesize(“test.txt”));
fclose($fp);
?>
It finds pathnames matching a pattern
Example
<?php
foreach (glob(“*.txt”) as $file) {
echo $file;
}
?>
Its an alias of fwrite() and writes to a file
Example
<?php
$fp = fopen(‘test.txt’, ‘w’);
fputs($fp, “phpcodez”);
fclose($fp);
?>
It format line as CSV and write to file pointer
Example
<?php
$data = array ( array(‘php’, ‘asp’, ‘jsp’, ‘js’));
$fp = fopen(‘test.csv’, ‘w’);
foreach ($data as $value) {
fputcsv($fp, $value);
}
fclose($fp);
?>
It outputs all remaining data on a file pointer
Example
<?php
$fp = @fopen(“test.txt”, “r”);
fpassthru($fp);
?>
It opens file or URL
Example
<?php
$fp = @fopen(“test.txt”, “r”);
if ($fp) {
while (($content = fgets($fp, 4096)) !== false) {
echo $content;
}
fclose($fp);
}
?>