[PHP-NOTES] note 130303 added to function.mime-content-type

[email protected] ("[email protected]")
Newsgroups php.notes
Message-ID <[email protected]>
сгенерировать актуальный массив MIME-типов , взяв их из официального файла Apache mime.types по URL:

<?php

// Правильная отправка заголовков
header('Content-Type: text/plain; charset=utf-8');

define('APACHE_MIME_TYPES_URL', 'http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types');

function generateUpToDateMimeArray($url) {
    $content = @file_get_contents($url);
    if (!$content) {
        return false;
    }

    $lines = explode("\n", $content);
    $mimeTypes = [];

    foreach ($lines as $line) {
        // Пропускаем пустые строки и комментарии
        if (trim($line) === '' || $line[0] === '#') {
            continue;
        }

        // Парсим строку
        preg_match_all('#([^\s]+)#', $line, $matches);
        $parts = $matches[1];

        if (count($parts) < 2) {
            continue;
        }

        $mimeType = $parts[0];
        for ($i = 1; $i < count($parts); $i++) {
            $ext = strtolower($parts[$i]);
            $mimeTypes[$ext] = $mimeType;
        }
    }

    return $mimeTypes;
}

$mimeArray = generateUpToDateMimeArray(APACHE_MIME_TYPES_URL);

if ($mimeArray !== false) {
    echo "<?php\n";
    echo "// Сгенерировано автоматически\n";
    echo '$mime_types = array(' . "\n";

    $output = [];
    foreach ($mimeArray as $ext => $type) {
        $output[] = "    '" . $ext . "' => '" . $type . "'";
    }

    echo implode(",\n", $output);
    echo "\n);\n";
} else {
    die("Не удалось загрузить или обработать mime.types");
}
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.137 (proxied: 2a00:1370:8178:cd2:e589:c0ad:6403:27f5)
----
Manual Page -- https://php.net/manual/en/function.mime-content-type.php
Edit        -- https://main.php.net/note/edit/130303
Del: integrated  -- https://main.php.net/note/delete/130303/integrated
Del: useless     -- https://main.php.net/note/delete/130303/useless
Del: bad code    -- https://main.php.net/note/delete/130303/bad+code
Del: spam        -- https://main.php.net/note/delete/130303/spam
Del: non-english -- https://main.php.net/note/delete/130303/non-english
Del: in docs     -- https://main.php.net/note/delete/130303/in+docs
Del: other reasons-- https://main.php.net/note/delete/130303
Reject      -- https://main.php.net/note/reject/130303
Search      -- https://main.php.net/manage/user-notes.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.