2016-10-02 6 views
1

こんにちは、私はforce_download関数に関する1つの問題があります。私はウェブサイトにアップロードフォームを持っています。この関数を使用してアップロードしたデータをダウンロードしています。codeigniterの画像とpdfファイルでforce_downloadを実行していません

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

しかし、pdf、png、jpgファイルは、ダウンロードする必要がない場合には直接表示できますが、この機能を使用するとすべてのファイルがダウンロードできます。

アップロードフォルダーへの直接リンクを使用しようとしましたが、ログインユーザーが何かをダウンロードできるだけのアクセスを拒否する.htaccessファイルがあるため、可能です。

+0

まず、ファイルの種類を確認する必要があります。それに応じて行動してください。コンテンツの処理方法を[このコード](http://stackoverflow.com/questions/38651094/access-file-outside-public-html-using-codeigniter-3-x/38658618#38658618)にチェックしてください。 – Tpojka

+0

ありがとう、この作品は今私は直接それをダウンロードせずにしたい場合は、png、jpg、mp4に似た何かがpdfファイルを直接見ることができます...?ブラウザに直接開くことができる他のファイルタイプがあります。 – 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

関連する問題