2016-10-02 2 views
1

안녕하세요. force_download 함수에 대한 한 가지 문제가 있습니다. 웹 사이트에 업로드 양식이 있는데이 함수를 사용하여 업로드 한 데이터를 다운로드하고 작동합니다.codeigniter에 이미지 및 pdf 파일이 포함 된 force_download가 없습니다.

public function download($file) 
    { 
     force_download('./uploads/'.$file, NULL); 
    } 

하지만 pdf, png, jpg 파일을 다운로드 할 필요가 없다면 navegator에서 직접 볼 수 있지만이 기능을 사용하면 모든 파일이 다운로드됩니다. 어떻게 가져올 수 있습니까?

내 업로드 폴더에 대한 직접 링크를 사용하려고하지만 로그인 사용자가 무언가를 다운로드 할 수 없도록하는 액세스를 거부 한 .htaccess 파일이 있기 때문에 가능할 수 있습니다.

+0

먼저 파일 형식을 확인해야합니다. 그에 따라 행동하십시오. 콘텐츠 처리 방법을 [이 코드] (http://stackoverflow.com/questions/38651094/access-file-outside-public-html-using-codeigniter-3-x/38658618#38658618)에서 확인하십시오. – Tpojka

+0

고마워 친구가 지금은 내가 그것을 다운로드하지 않고 싶다면 직접 PDF 파일을 볼 수있는 작품은 PNG, JPG, MP4와 비슷한 뭔가가 ...? 브라우저에서 직접 열 수있는 다른 파일 형식이 있습니다. – htmlpower

답변

1

내가 이미 작성한대로 if elseif else 또는 더 나은 switch case 블록을 다운로드/미리보기 코드 전에 확인하여 차단하십시오. 예 :

public function download($file) 
{ 
    //get the file extension 
    $info = new SplFileInfo($file); 
    //var_dump($info->getExtension()); 

    switch ($info->getExtension()) { 
     case 'pdf': 
     case 'png': 
     case 'jpg': 
      $contentDisposition = 'inline'; 
      break; 
     default: 
      $contentDisposition = 'attachment'; 
    } 

    if (file_exists($file)) { 
     header('Content-Description: File Transfer'); 
     header('Content-Type: application/pdf'); 
     // change inline to attachment if you want to download it instead 
     header('Content-Disposition: '.$contentDisposition.'; filename="'.basename($file).'"'); 
     header('Expires: 0'); 
     header('Cache-Control: must-revalidate'); 
     header('Pragma: public'); 
     header('Content-Length: ' . filesize($file)); 
     readfile($file); 
    } 
    else echo "Not a file"; 
} 
+0

감사합니다. 이제는 모든 확장명으로 작동합니다. – htmlpower

관련 문제