2014-10-30 87 views
0

這裏我有一些代碼,它的確如它所說和下載,但是隻下載一個白色的空白空文件,並且我試圖使用這一個Wordpress並且必須沒有插件。這個腳本可以工作,但不會將任何信息下載到PDF文檔上,它只是一個白色的空白頁面,代碼中,尋找一個解決方案來將Wordpress文章添加到文檔中,這裏是鏈接和代碼,理想情況下我希望它下載執行鏈接的頁面。下載PHP/HTML/CSS到PDF文檔

HTML鏈接

<a href="http://www.WEBSITE.com/wp-content/themes/the-theme/download.php?download_file=some_file.pdf">Download file</a> 

的download.php

<?php 

ignore_user_abort(true); 

$path = ""; // change the path to fit your websites document structure 
$dl_file = preg_replace("([^\w\s\d\-_~,;:\[\]\(\].]|[\.]{2,})", '', $_GET['download_file']); // simple file name validation 
$fullPath = $path.$dl_file; 

if ($fd = fopen ($fullPath, "r")) { 
$fsize = filesize($fullPath); 
$path_parts = pathinfo($fullPath); 
$ext = strtolower($path_parts["extension"]); 
switch ($ext) { 
    case "pdf": 
    header("Content-type: application/pdf"); 
    header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a file download 
    break; 
    // add more headers for other content types here 
    default; 
    header("Content-type: application/octet-stream"); 
    header("Content-Disposition: filename=\"".$path_parts["basename"]."\""); 
    break; 
} 
header("Content-length: $fsize"); 
header("Cache-control: private"); //use this to open files directly 
while(!feof($fd)) { 
    $buffer = fread($fd, 2048); 
    echo $buffer; 
} 
} 
fclose ($fd); 
exit; 
?> 

回答

0

不要使用上面的代碼!

它不僅不會將任何東西轉換爲PDF格式,而且還允許世界以純文本格式下載網絡服務器可讀的任意文件。所以你可以下載你的mysql數據庫證書或者你的FTP密碼(如果在wp-config.php中設置的話)。

說明:代碼不會轉換任何東西。它只需要一個任意文件的內容並將其輸出爲PDF或八位字節流內容類型。拿一個文本編輯器,看看你下載的「PDF」。


看起來你還沒有設定線 $path = ""; // change the path to fit your websites document structure到指向some_file.pdf的完整路徑。

+0

它下載文件,但它是一個空白的pdf,我也看了看路徑,但它只在空白時下載,如果它有一個結構崩潰並給出多個錯誤 – user3348182 2014-10-30 14:34:19