Índice

Files

Provide a simple temporary or permanent data store. There are two major types of functions:
f* () :  Functions that work with a file resource, e.g. fopen()
file* (): Functions that work with a filename, e.g. file_get_contents()

Functions with File Resources

Example:

$filename = "c:\\files\\somepic.gif";
$handle = fopen($filename, "r+b");
$contents = fread($handle, filesize($filename));
fwrite($handle,"hello error");
fclose($handle);

$handle = fopen("http://www.example.com/", "rb");  // remote file
$contents = '';
while (!feof($handle)) {
$contents .= fread($handle, 8192);  // 8192 is the default maximum chunk
}
fclose($handle);

 

Functions with Filenames and Directories

Example:

if ($handle = opendir('/path/to/files')) {
while (false !== ($entry = readdir($handle))) {
echo "$entry\n";
}
closedir($handle);
}
if ($d = dir('/path/to/files')) { // directory object
while (false !== ($entry = $d->read())) {
echo "$entry\n";
}
$d->close();
}

 

Links

File Permissions

Fileinfo

Example:

$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type extension
foreach (glob("*") as $filename) {
echo finfo_file($finfo, $filename) . "\n";
}
finfo_close($finfo);

Constants

File Modes

Unix based systems use \n as the line ending character, Windows based systems use \r\n as the line ending characters and Macintosh based systems use \r as the line ending character. For correcting the discrepancies, additional mode flags can be specified:

Locks

Streams

Protocols and Wrappers

Pipelines / Transport

Contexts

<?php
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n"
)
);
$context = stream_context_create($opts);
/* Sends an http request to www.example.com
   with additional headers shown above */
$fp = fopen('http://www.example.com', 'r', false, $context);
fpassthru($fp); fclose($fp);

Filters

Stream Functions

Sockets

<?php
$fp = fsockopen("example.preinheimer.com", 80, $errno, $errstr, 30)