note 52722 added to function.readfile
| From: | PhilippHeckel at osu1 dot php dot net | Date: | Tue, 10 May 2005 23:58:33 +0000 |
| Subject: | note 52722 added to function.readfile | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-89234@lists.php.net to get a copy of this message | ||
To use readfile() it is absolutely necessary to set the mime-type before. If you are using an
Apache, it's quite simple to figure out the correct mime type. Apache has a file called
"mime.types" which can (in normal case) be read by all users.
Use this (or another) function to get a list of mime-types:
<?php
function mimeTypes($file) {
if (!is_file($file) || !is_readable($file)) return false;
$types = array();
$fp = fopen($file,"r");
while (false != ($line = fgets($fp,4096))) {
if (!preg_match("/^\s*(?!#)\s*(\S+)\s+(?=\S)(.+)/",$line,$match)) continue;
$tmp = preg_split("/\s/",trim($match[2]));
foreach($tmp as $type) $types[strtolower($type)] = $match[1];
}
fclose ($fp);
return $types;
}
# [...]
# read the mime-types
$mimes = mimeTypes('/usr/local/apache/current/conf/mime.types');
# use them ($ext is the extension of your file)
if (isset($mimes[$ext])) header("Content-Type: ".$mimes[$ext]);
header("Content-Length: ".@filesize($fullpath));
readfile($fullpath); exit;
?>
If you do not want to read from the mime.types file directly, you can of course make a copy in
another folder!
Cheers Philipp Heckel
----
Manual Page -- http://www.php.net/manual/en/function.readfile.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+52722
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52722&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52722&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52722&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52722&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52722&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+52722&report=yes
Search -- http://master.php.net/manage/user-notes.php