2017-03-15 14 views
0

웹상에서 좋은 답변을 찾지 못했습니다.쉬운 조건문 파일 정보

저는 symfony2.5 및 php 5.3을 사용하고 있으며 파일 탐색기 응용 프로그램을 만들고 있습니다.

좋은 연관 콘텐츠 유형을 적용하기 전에 파일의 확장자를 알고 싶습니다.

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; 
    } 
} 

내가 프로그래밍을 시작 해요 : mime_content_type 기능이

가 여기 내 showAction {}이다 .. 사용되지 않습니다, 난 파일이 엑셀 하나 또는 PDF 파일의 경우 테스트하는 기능이 필요합니다. 너희들 나를 도울 수 있니? 감사합니다.

+0

당신은 응답 객체를 반환해야합니다 당신은 다음, finfo 리소스를 만들과 같이, 파일을 검사하기 위해 해당 자원을 사용해야합니다. – COil

+0

내 상태가 작동하지 않아 게시 된 게시물 –

+0

파일 확장명을 테스트하지 않는 이유는 무엇입니까? – Edwin

답변

0

finfo 조금 이상합니다. 의 OOP 방식

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

또는 :

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

1) 정의되지 않은 함수 'finfo_open'을 호출하고 2) 'finfo'클래스를 찾을 수 없습니다. –

+0

php_ini에서 php_fileinfo.dll 확장 기능을 활성화했으며 훌륭하게 작동합니다! 고마워요 @ 헤이건 건포도 –