Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the mime type of a file after using file_get_contents from a remote server

I'm reading a file in PHP from Alfresco and then outputting it to the browser. The only problem is the mime type or the extension of the file. This is the code I'm using:

<?php
ob_start();
require_once("libs/AlfrescoConnect.php");

$nomeFile = rawurldecode($_GET['nomeFile']);    
$urlDownload = $_GET['urlDownload'];
$fileDownloadUrl =
    AlfrescoConnect::$serverPath. $urlDownload .
    "&attach=true&alf_ticket=".AlfrescoConnect::getTiket();
fb($fileDownloadUrl);

$cnt = file_get_contents($fileDownloadUrl);

header("Content-type: Application/octet-stream");
header('Cache-Control: must-revalidate');
header('Content-disposition: attachment; filename=' .$nomeFile);
echo($cnt);
exit();

echo("Impossibile trovare il file");

I receive the name of the file from the get because, I don't know how to get the name from alfresco. But I have to guess the mimetype somehow. If I echo $cnt in the first characters, there are references to the fact that it is a PDF (for example on screen I see

%PDF-1.3 %âãÏÓ 2 0 obj << /Length 3 0 R /Filter /CCITTFaxDecode /DecodeParms << /K 0 /Columns 2480 /Rows 3508 >> /Type /XObject /Subtype /Image /Width 2480 /Height 3508 /BitsPerComponent 1 /ColorSpace /DeviceGray >> stream

So there must be a way to get the mime type from it with a function.

Any help is appreciated!

Edit. If anyone is interested here is a class that you can use to get the extension from the mime type. http://www.ustrem.org/en/articles/mime-type-by-extension-en/

like image 703
Nicola Peluchetti Avatar asked Jan 12 '11 17:01

Nicola Peluchetti


People also ask

How do I determine the MIME of a file?

For detecting MIME-types, use the aptly named "mimetype" command. It has a number of options for formatting the output, it even has an option for backward compatibility to "file". But most of all, it accepts input not only as file, but also via stdin/pipe, so you can avoid temporary files when processing streams.

Where is MIME type stored in a file?

All MIME type information is stored in a database. The MIME database is located in the directory /usr/share/mime/ . The MIME database contains a large number of common MIME types, stored in the file /usr/share/mime/packages/freedesktop. org.

What is MIME type to download any type of file?

MIME types enable browsers to recognize the filetype of a file which has been sent via HTTP by the webserver. As a result the browser is able to choose a suitable displaying method. Common MIME types are for example text/html for html-files or image/jpeg for jpeg-files.

What is a file's MIME type?

A media type (also known as a Multipurpose Internet Mail Extensions or MIME type) indicates the nature and format of a document, file, or assortment of bytes. MIME types are defined and standardized in IETF's RFC 6838.


1 Answers

You can use the finfo::buffer() method: http://php.net/finfo_buffer.

<?php
$finfo = new finfo(FILEINFO_MIME);
echo $finfo->buffer($cnt) . PHP_EOL;

NOTE: You could optionally use the finfo_buffer procedural function if that suites you better than using the object-oriented methodology.

like image 154
Wil Moore III Avatar answered Oct 14 '22 11:10

Wil Moore III