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 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);
}
?>
Match filename against a pattern
Example
<?php
$color =”phpcodez.txt”;
if (fnmatch(“*phpcode[zs].txt”, $color)) {
echo “phpcodez”;
}
?>
Output
phpcodez
It can be used to lock the file
Example
<?php
$fp = fopen(“test.txt”, “r+”);
flock($fp, LOCK_EX); // acquire an exclusive lock (writer).
flock($fp, LOCK_SH); // acquire a shared lock (reader).
flock($fp, LOCK_UN); // unlock the file
fclose($fp);
?>
It returns file type
Example
<?php
echo filetype(“test.txt”);
?>
Output
file