2011-12-08 9 views
0
if($_POST['mode']=="save") { 
$root = $_SERVER['DOCUMENT_ROOT']; 
$path = "/mcboeking/"; 
$path = $root.$path; 
$file_path = $_POST['path']; 
$file = $path.$file_path; 

if(!file_exists($file)) { 
    die('file not found'); 
} else { 
    header("Cache-Control: public"); 
    header("Content-Description: File Transfer"); 
    header('Content-Type: application/force-download'); 
    header("Content-Disposition: attachment; filename=\"".basename($file)."\";"); 
    header("Content-Length: ".filesize($file)); 
    readfile($file);}} 

ファイルをダウンロードして開くと、エラーメッセージが表示されます。 .docファイルを開こうとすると、ファイル構造が無効であるというメッセージが表示されます。 .jpgファイルを開こうとすると:このファイルを開くことはできません。破損している可能性があります。または、プレビューが認識できないファイル形式です。ダウンロードファイルを強制的に破損しましたか?

しかし、私はPDFファイルをダウンロードしても問題なく開きます。

誰かが私を助けることができますか?

p.s.私は別のヘッダを試しました: header( 'Content-Type:application/octet-stream');

答えて

0
 
$fsize = filesize($yourFilePath); 
$allowed_ext = array (

    // archives 
    'zip' => 'application/zip', 

    // documents 
    'pdf' => 'application/pdf', 
    'doc' => 'application/msword', 
    'xls' => 'application/vnd.ms-excel', 
    'ppt' => 'application/vnd.ms-powerpoint', 

); 
$mtype = mime_content_type($file_path); 
if ($mtype == '') { 
    $mtype = "application/force-download"; 
} 
header("Pragma: public"); 
header("Expires: 0"); 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Cache-Control: public"); 
header("Content-Description: File Transfer"); 
header("Content-Type: $mtype"); 
header("Content-Disposition: attachment; filename=\"$asfname\""); 
header("Content-Transfer-Encoding: binary"); 
header("Content-Length: " . $fsize); 
//then ur rest code 
0

ヘッダーは実際のコンテンツとは関係がないため、実際のコンテンツに問題があることが問題です。

また、実際にContent-Typeヘッダーを変更して強制的にダウンロードを試みるべきではありません。それはそれがされているものを残す。クライアントがそれをどうするかを決定しましょう。

0

あなたはreadfile()データが、何も出力しないことを確認します、PHPコードはPHPファイルの非常に最初のシンボルで<?で始まることを確認し、何のシンボルは、ファイルの末尾に決算?>後がないことスペースや改行のように(または?>を完全に削除する)。

0

PHPファイルをNOTEPADで開きます。 「名前を付けて保存」を押し、「ASCI」ファイル形式を選択します。これは私の場合に役立ちました。なぜならファイルはUTF8形式であり、これが原因でした。

関連する問題