i'm reading a file from php from Alfresco and then outputting it to the browser. The only ptoblem is the mimetype or the extension of the file. This is the code i'm using:
<?php
ob_start();
//require_once("libs/FirePHPCore/fb.php");
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 becausa 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 firsat caracters 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_tipe from it with a function.
Any help is apprecieted!
Edit. If anyone is intereste 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/
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.