note 117829 added to function.mime-content-type
| From: | jacopo dot mazzoni at gmail dot com | Date: | Sun, 16 Aug 2015 09:06:56 +0000 |
| Subject: | note 117829 added to function.mime-content-type | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-203363@lists.php.net to get a copy of this message | ||
Slight improvement over Josh Sean's code: this makes a usable and formatted php file
containing the latest mime associations ( strictly using the file extension) in an array or if the
file_get_contents fails ( say you are offline ) leaves the original file alone.
I made it a bit "dummer" for readability purposes, don't judge me just improve it on
your own.
<?php
define('APACHE_MIME_TYPES_URL','http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types');
function generateUpToDateMimeArray($url){
$s=array();
$result = @file_get_contents($url);
if ($result == FALSE )
{
$returned = "ERROR";
}
else
{
foreach(@explode("\n",$result)as $x)
{
if(isset($x[0])&&$x[0]!=='#'&&preg_match_all('#([^\s]+)#',$x,$out)&&isset($out[1])&&($c=count($out[1]))>1)
for($i=1;$i<$c;$i++)
$s[]=' \''.$out[1][$i].'\' =>
\''.$out[1][0].'\'';
}
$returned = @sort($s)?'<?php' . "\n" . '$mime_types = array('
. "\n" . implode($s,",\n") . "\n);\n" . '?>':false;
}
return $returned;
}
$file_name = 'mime-array.php';
$data = generateUpToDateMimeArray(APACHE_MIME_TYPES_URL);
if ($data != "ERROR")
{
$file = fopen($file_name, 'wb') or die("cannot open $file_name\n");
fwrite($file, $data ) or die("cannot write data\n");
fclose($file);
echo "updated";
}
else
{
echo "faliure";
}
?>
All you need to do is run it once and in your code you can simply add these two lines:
<?php
include 'mime-array.php';
global $mime_types;
?>
enjoy
----
Server IP: 216.194.115.244
Probable Submitter: 94.163.216.203
----
Manual Page -- http://php.net/manual/en/function.mime-content-type.php
Edit -- https://master.php.net/note/edit/117829
Del: integrated -- https://master.php.net/note/delete/117829/integrated
Del: useless -- https://master.php.net/note/delete/117829/useless
Del: bad code -- https://master.php.net/note/delete/117829/bad+code
Del: spam -- https://master.php.net/note/delete/117829/spam
Del: non-english -- https://master.php.net/note/delete/117829/non-english
Del: in docs -- https://master.php.net/note/delete/117829/in+docs
Del: other reasons-- https://master.php.net/note/delete/117829
Reject -- https://master.php.net/note/reject/117829
Search -- https://master.php.net/manage/user-notes.php