2017-09-03 82 views
0

我正在創建一個WordPress插件,當按下按鈕時它將生成HTML頁面。這樣可行;該問題不是與WordPress相關的,儘管代碼位於WP插件中。下一步是給用戶提示下載/打開已創建的文件。Force下載生成的HTML

基礎上研究這裏和其他地方,這個過程應該創建一個下載/打開提示:

/* another process creates an HTML file "somefile.html", and stores it 
in the plugin folder; that is done with an fopen/fwrite/fclose. 
This process is started by a button on the plugin settings page. 
When the button is clicked, the "somefile.html" is created. 
So at this point, the HTML file is created and stored in the plugin folder */ 

    $size = filesize($thefile); 
    header('Content-Description: File Transfer'); 
    header('Content-Type: application/octet-stream'); 
    header('Content-Disposition: attachment; filename='somefile.html'); 
    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: ' . $size); 
    exit; 

過程(由插件設置頁面上的按鈕,點擊開始)將創建一個HTML文件,它正確存儲在插件文件夾中(文件位置不是問題,將在最終版本中解決)。然後我看到打開/保存文件對話框。

如果我從服務器打開生成的HTML文件,則HTML與預期的一樣。但是,如果我通過打開/保存提示打開生成的文件,我會看到插件設置頁面的表示,而不是生成的HTML。

我懷疑我需要一個obflush()/ flush(),但在標題行之前放置這些語句不能解決問題。

所以我的問題是,打開/另存爲對話框不讀取存儲在服務器上的'somefile.html'。我用打開的對話框得到一個插件設置HTML頁面。

如何確保我創建的HTML文件通過打開/保存對話框打開? (請注意,儘管代碼位於WordPress插件內部,但問題並不僅限於WordPress,代碼只是創建一個表單按鈕;在提交表單操作時會創建一個HTML文件並將其保存到服務器上,然後頭」語句用來創建一個保存/打開對話框。)

ADDED

此代碼應顯示進程。這是用於創建somefile.html文件的'有效'HTML頁面。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head> 
    <body> 
    This is the page with some content. It will create the HTML page (the 'xoutput' content). 
    </body> 
<!--- the above is the page that is initially displayed --> 
    <?php 
    // now we create the content of the generated/saved file  
    $xoutput = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head> 
    <body>'; 
    $xoutput .= 'There is some content generated here. Actual content doesn't matter.'; 
    $xoutput .= '</body> </html>'; 
     $thefile = "outputfile.html"; 
     $handle = fopen($thefile, "w"); 
     fwrite($handle, $xoutput); 
     fclose($handle); 
    $quoted = sprintf('"%s"', addcslashes(basename($thefile), '"\\')); 
    $size = filesize($thefile); 
    // now that the somefile.html has been created and stored, let's create the open/save dialog 
    header('Content-Description: File Transfer'); 
    header('Content-Type: application/octet-stream'); 
    header('Content-Disposition: attachment; filename=' . $quoted); 
    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: ' . $size); 

     exit; 
     return; 

當您加載頁面時,您會看到'這裏有一些內容生成'HTML頁面,而不是'somefile.html'內容。

+0

你們能通過瀏覽器somefile.html達到? – deg

+0

我可以看到服務器上的somefile.html是我生成的文件(通過我的代碼編輯器)。問題是與打開的對話框顯示的文件是插件頁面,而不是生成的文件。 –

+0

不,我的意思是你的瀏覽器,你可以打開somefile.html通過網址?我試圖遠離CMS,但我記得有時訪問內部文件夾受到htaccess文件和其他內容的限制。 – deg

回答

1

新鮮的嘗試和測試:

if (file_exists($thefile)) { 
    header('Content-Description: File Transfer'); 
    header('Content-Type: application/octet-stream'); 
    header('Content-Disposition: attachment; filename='.basename($thefile)); 
    header('Content-Transfer-Encoding: binary'); 
    header('Expires: 0'); 
    header('Cache-Control: must-revalidate'); 
    header('Pragma: public'); 
    header('Content-Length: '.filesize($thefile)); 
    ob_clean(); 
    flush(); 
    readfile($thefile); 
    exit; 
} 
+0

是的,那樣做。感謝您的額外時間! –

+0

儘管上面的代碼有效(謝謝,@deg),但是出現了一個錯誤:請參閱https:// stackoverflow。com/questions/47106803/header-output-doesnt-show-open-save-dialog-on-one-site。 –