2014-02-21 192 views
0

你能告訴我我的頭文件有什麼問題嗎?我的下載無效。我用PHPWord庫,但我不認爲這是一個問題無法下載word文件

<?php 

    require_once 'PHPWord.php'; 
    $PHPWord = new PHPWord(); 

    $section = $PHPWord->createSection(); 
    $wordText = utf8_encode($_REQUEST['TEXT']); 

    $section->addText($wordText); 

    $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); 
    //$objWriter->save('helloWorld.docx'); 
    $path = 'tmp/kikou2.docx'; 
    $objWriter->save($path); 
    //download code 
    header('Content-Description: File Transfer'); 
    header('Content-Type: application/octet-stream'); 
    header('Content-Disposition: attachment; filename=' .$path); 
    header('Content-Transfer-Encoding: binary'); 
    header('Expires: 0'); 
    header('Content-Length: ' . filesize($objWriter)); 
    readfile($objWriter); 
    unlink($objWriter); // deletes the temporary file 
    exit; 


?> 

感謝

回答

1

建議標題爲OfficeOpenXML .docx文件

// Redirect output to a client’s web browser (.docx) 
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document'); 
header('Content-Disposition: attachment;filename="kikou2.docx"'); 
header('Cache-Control: max-age=0'); 
// If you're serving to IE 9, then the following may be needed 
header('Cache-Control: max-age=1'); 

// If you're serving to IE over SSL, then the following may be needed 
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past 
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified 
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1 
header ('Pragma: public'); // HTTP/1.0 

付費特惠注意Content-Type標題

+0

它不起作用(其他人的回答也不太...),你有什麼想法嗎? – So4ne

+0

我不好使用POST ajax請求,它不允許我調用該文件。但你的回答很好,很有幫助,謝謝! – So4ne

1

添加兩個頭:

header("Content-Type: application/force-download"); 
header("Content-Type: application/download"); 
+0

使用多個內容類型標題是沒有意義的;只有最後一個實際應用 - 嘗試這個,然後檢查瀏覽器收到的標題,看看我的意思 –

1
header('Content-Description: File Transfer'); 
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document'); 
header('Content-Disposition: attachment; filename="'.basename($objWriter).'"'); //<<< Note the " " surrounding the file name 
header('Content-Transfer-Encoding: binary'); 
header('Connection: Keep-Alive'); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
header('Pragma: public'); 
header('Content-Length: ' . filesize($objWriter)); 
+0

使用多個內容類型標題是沒有意義的;只有最後一個實際應用 - 嘗試這一點,然後檢查瀏覽器收到的標題,看看我的意思....但很榮幸解決OP的代碼 –

+0

@mark:謝謝你的內容處理文件名問題。回答編輯! –

0
  $document->save($url.'temp/Form Letters1.docx'); 
      $path = $url.'temp/Form Letters1.docx'; 
      header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document'); 
      header('Content-Disposition: attachment;filename="Form Letters1.docx"'); 
      header('Expires: 0'); 
      header('Cache-Control: must-revalidate'); 
      header('Pragma: public'); 
      header('Content-Length: ' . filesize($path)); 
      ob_clean(); 
      flush(); 
      readfile($path);