2016-07-29 3 views
1

を使用してpublic_htmlの外部にアクセスするファイルセキュリティの理由から、私はPdfpublic_htmlの外にあるフォルダにPDFファイルを持っています。codeigniter 3.X

私のコントローラからアプリケーションフォルダ内にあるこのファイルにアクセスしようとしています。 私はいくつかのパスを使って試しました..

1つは:../../../../Pdf/{$name_hash}.pdfです。 もう1つは:/home/xx/Pdf/{$name_hash}.pdf

ファイルを含めてjs.openwindowとして送信しようとしましたが、readfile($filepath)は役に立たなくなりました。

ファイルが存在し、名前もハッシュ関数によって正しく生成されるので、問題を設定しているパスが正しいと確信しています。

パスを設定するために私が従わないCIのルールはありますか?またはこれに他の解決策があります..助けてください!

答えて

1

ブラウザURL内でpublic_html(または仮想ホストがドメインを設定するディレクトリ)の背後にあるファイルにアクセスできないということです。あなたはファイルの内容を取得し、バッファから出力に送る必要があります。 example

public function pdf() 
{ 
    // you would use it in your own method where $name_hash has generated value 
    $file = "/home/xx/Pdf/{$name_hash}.pdf"; 

    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: inline; filename="'.basename($file).'"'); 
     header('Expires: 0'); 
     header('Cache-Control: must-revalidate'); 
     header('Pragma: public'); 
     header('Content-Length: ' . filesize($file)); 
     readfile($file); 
    } 
    else "Can not read the file"; 
} 

PHP docs:あなたはそのためのreadfile($file) PHPの作り付けの機能を使用することができます。