2016-09-28 3 views
0

私はソースが表示されていないファイルのダウンロードを可能にするために次のコードを書いています。PHPファイルダウンヘッダ()

この例では、ファイル 'test.pdf'はダウンロードされていますが、破損しています。

これはどのような理由が考えられますか?

$path = 'http://www.domain.org/images/uploads/test.pdf'; 

// Directory Path 
$directory_path_filename = str_replace('http://www.domain.org/', '', $path); 

$filepath = '/var/sites/l/domain.org/public_html/'.$directory_path_filename; 

if (file_exists($filepath)) { 

    $finfo = finfo_open(FILEINFO_MIME); 
    header('Content-Type: application/pdf'); 
    header('Content-Disposition: attachment; filename= ' . basename($filepath)); 
    header('Content-Length: ' . filesize($filepath)); 
    header('Expires: 0'); 
    finfo_close($finfo); 

    ob_clean(); 
    flush(); 
    readfile($filepath); 
    exit; 
} 

答えて

0

この方法

$ファイルを使用してくださいは、完全なパスを受け、$ファイル名は、ダウンロードファイル名です。

public static function downloadFile($file, $filename) 
    { 
     if (file_exists($file)) { 
      header('Content-Description: File Transfer'); 
      header('Content-Type: application/octet-stream'); 
      header('Content-Disposition: attachment; filename = '.basename($filename)); 
      header('Content-Transfer-Encoding: binary'); 
      header('Expires: 0'); 
      header('Cache-Control: must-revalidate, post-check = 0, pre-check = 0'); 
      header('Pragma: public'); 
      header('Content-Length: ' . filesize($file)); 
      ob_clean(); 
      flush(); 
      readfile($file); 
      die; 
     } 
    } 
+0

こんにちは。ありがとうございますが、これでも同じ問題が発生します。ダウンロードしたファイルは一度開くことはできません。 – ccdavies

+0

そのような場合はおそらく、ヘッダーの前にいくつかの出力があります... noiteや何かのように、完全なコードを投稿してください。 –