Utilities::titleMimeType PHP Method

titleMimeType() static public method

To reduce search time the function checks first wether the file has a well known extension. If not two functions are tried. If all fails 'application/force-download' is returned to force the download of the unknown format.
static public titleMimeType ( string $file_path ) : string
$file_path string path to ebook file
return string MIME type
    static function titleMimeType($file_path)
    {
        $mtype = '';
        if (preg_match('/epub$/', $file_path) == 1) {
            return Utilities::MIME_EPUB;
        } else {
            if (preg_match('/mobi$/', $file_path) == 1) {
                return 'application/x-mobipocket-ebook';
            } else {
                if (preg_match('/azw3?$/', $file_path) == 1) {
                    return 'application/vnd.amazon.ebook';
                } else {
                    if (preg_match('/pdf$/', $file_path) == 1) {
                        return 'application/pdf';
                    } else {
                        if (preg_match('/txt$/', $file_path) == 1) {
                            return 'text/plain';
                        } else {
                            if (preg_match('/html$/', $file_path) == 1) {
                                return 'text/html';
                            } else {
                                if (preg_match('/zip$/', $file_path) == 1) {
                                    return 'application/zip';
                                }
                            }
                        }
                    }
                }
            }
        }
        if (function_exists('mime_content_type')) {
            $mtype = mime_content_type($file_path);
        } else {
            if (function_exists('finfo_file')) {
                $finfo = finfo_open(FILEINFO_MIME);
                $mtype = finfo_file($finfo, $file_path);
                finfo_close($finfo);
            }
        }
        if ($mtype == '') {
            $mtype = 'application/force-download';
        }
        return $mtype;
    }

Usage Example

Example #1
0
 function testTitleMimeType()
 {
     $this->assertEqual('application/epub+zip', Utilities::titleMimeType('x/y/test.epub'));
     $this->assertEqual('application/vnd.amazon.ebook', Utilities::titleMimeType('test.azw'));
     $this->assertEqual('application/x-mobipocket-ebook', Utilities::titleMimeType('test.mobi'));
     $this->assertEqual('text/plain', Utilities::titleMimeType(self::FIXT . '/test.unknown-format'));
     $this->assertEqual('application/xml', Utilities::titleMimeType(self::FIXT . '/atom.rng'));
 }
All Usage Examples Of Utilities::titleMimeType