Search code examples
phpsymfonyconditional-statementscontent-typefileinfo

Easy one condition statement file info


i didnt found a good answer on the web..

I'm using symfony2.5 and php 5.3 and creating a file explorer application.

I want to know the extension of the file before apply the good associated content-type. mime_content_type function is deprecated..

Here is my showAction{} , i need a function who test if the file is an excel one or a pdf file :

public function showAction($repertoire, $file)
{

    $response = new Response();
    $response->setContent(file_get_contents(''.$repertoire.'/'.$file.''));

    if(file_info($file) == 'application/pdf'){

    $response->headers->set('Content-Type', 'application/pdf');
    $response->headers->set('Content-disposition', 'filename='. $file);
    return $response;

    }else {

    $response->headers->set('Content-Type', 'application/application/vnd.ms-excel');
    $response->headers->set('Content-disposition', 'filename='. $file);
    return $response;
    }
}

I'm starting with programming. Can you help me guys ? Thanks a lot !


Solution

  • finfo is a little strange. You have to create a finfo resource, then use that resource to inspect a file, like so:

    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    if('application/pdf' === finfo_file($finfo, $file)) {
        ...
    }
    

    or, the OOP way:

    $finfo = new finfo(FILEINFO_MIME_TYPE);
    if('application/pdf' === $finfo->file($file)) {
        ...
    }