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/
Use cURL instead of file_get_contents, then you can see the response header, which will hopefully have the mime type.
Or you could try using this http://www.php.net/manual/en/ref.fileinfo.php or this deprecated function http://php.net/manual/en/function.mime-content-type.php