Skip to content

Instantly share code, notes, and snippets.

@reinislejnieks
Forked from jonathantneal/file_get_mime.php
Created August 11, 2018 22:25
Show Gist options
  • Save reinislejnieks/d03915638d94c8b6e22ba740b636d23c to your computer and use it in GitHub Desktop.
Save reinislejnieks/d03915638d94c8b6e22ba740b636d23c to your computer and use it in GitHub Desktop.
PHP Goodies
<?php
function file_get_mime($filename) {
$extension = pathinfo($filename, PATHINFO_EXTENSION);
$mimes = array(
'audio/mp4' => 'm4a|f4a|f4b',
'audio/ogg' => 'oga|ogg',
'audio/&' => 'mid|midi|mp3|wav',
'application/javascript' => 'js|jsonp',
'application/json' => 'json',
'application/msword' => 'doc|dot',
'application/octet-stream' => 'bin',
'application/postscript' => 'ai',
'application/vnd.openxmlformats-officedocument.presentationml.presentation' => 'pptx',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'xlsx',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'docx',
'application/vnd.ms-excel' => 'xla|xls|xlt',
'application/vnd.ms-fontobject' => 'eot',
'application/vnd.ms-powerpoint' => 'pot|ppa|pps|ppt',
'application/x-shockwave-flash' => 'swf',
'application/xml' => 'atom|rdf|rss|xml',
'application/&' => 'pdf|rtf|zip',
'font/opentype' => 'otf',
'font/&' => 'ttf|ttc|woff',
'image/jpeg' => 'jpe|jpeg|jpg',
'image/svg+xml' => 'svg|svgz',
'image/vnd.adobe.photoshop' => 'psd',
'image/vnd.microsoft.icon' => 'ico',
'image/&' => 'bmp|ief|gif|png|tif|tiff|webp',
'text/cache-manifest' => 'appcache|manifest',
'text/plain' => 'txt',
'text/x-component' => 'htc',
'text/x-vcard' => 'vcf',
'text/&' => 'css|html|php|vtt',
'text/x-&' => 'markdown|md',
'video/mp4' => 'mp4|m4v|f4v|f4p',
'video/ogg' => 'ogv',
'video/quicktime' => 'mov|qt',
'video/&' => 'avi|mpg|vdo|viv|vivo|webm',
'video/x-&' => 'flv'
);
foreach ($mimes as $type => $exts) if (preg_match('/^('.$exts.')$/', $extension)) return preg_replace('/&/', $extension, $type);
return 'text/html';
}
<?php
function header_cache($time) {
$headLastModified = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
$fileLastModified = gmdate('D, d M Y H:i:s T', $time);
header('Cache-Control: public');
header('Last-Modified: '.$fileLastModified);
$lastModifiedMatches = $fileLastModified == $headLastModified;
if ($lastModifiedMatches) header($_SERVER['SERVER_PROTOCOL'].' 304 Not Modified', true, 304);
return $lastModifiedMatches;
}
<?php
function readfile_chunked($filename, $return_bytes = true) {
$buffer = '';
$bytes = 0;
$handle = fopen($filename, 'rb');
if ($handle === false) return false;
while (!feof($handle)) {
$buffer = fread($handle, 1024 * 1024);
print($buffer);
ob_flush();
flush();
if ($return_bytes) $bytes += strlen($buffer);
}
$status = fclose($handle);
if ($return_bytes && $status) return $bytes;
return $status;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment