Searched for tag MIME and found 2 results in 0.6 ms

PHP: Get the mime type for a file

If you need to get the mime type for a file in PHP you can use this function which wrappers the finfo_open() function.

function mime_from_filename($filename) {
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $mime  = finfo_file($finfo, $filename);
    finfo_close($finfo);

    return $mine;
}

Note: This function requires the file to exist on the disk.

Tags:
Leave A Reply

PHP: Simplify sending JSON to a browser

I can never remember the correct MIME type for JSON so I wrote this wrapper function to simplify sending JSON to a browser.

// Convert a PHP data structure to JSON and send it to the browser
function json_output($obj) {
    $output = json_encode($obj, JSON_INVALID_UTF8_SUBSTITUTE);

    // http://tools.ietf.org/html/rfc4627
    header('Content-type: application/json');
    print $output;
    exit;
}
Tags:
Leave A Reply