2012-12-24 19 views
2

下載鏈接:傳遞值到PHP文件更改文本里面的文件,然後強制下載同一個文件

<a href="download.php?words=2000&sort=popular&type=xml">Download php file</a> 

的download.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
<title>Home</title> 
</head> 
<body> 
<h1>PHP file</h1> 

<?php 
function get_text($text) 
{ 
    .... 
}  

function get_time($time) 
{ 
    .... 
}  

$url = "http://api.xxx.com/info.php?words=".$_GET['words']."&sort=".$_GET['sort']."&type="$_GET['type']; 
$xml = simplexml_load_file($url); 
if (count($xml)) 
{ 
    foreach($xml->book as $book) 
    { 
    echo ....  
    } 
} 
?> 

</body> 
</html> 

的的download.php是由就緒API php腳本提供網站管理員上傳到他們的FTP。網站管理員可以從表單中選擇多個選項(例如:download.php?words = 2000 & sort = popular & type = xml),然後提交表單以獲取其自定義API腳本。

這是將替換選項行,他們提交表單:

$url = "http://api.xxx.com/info.php?words=".$_GET['words']."&sort=".$_GET['sort']."&type="$_GET['type']; 

這是代碼強制下載後。但我不知道如何用$ content =「」;來包裝整個頁面。我知道如何包裝HTML代碼,但如何在頁面上包裝PHP函數和代碼?

header("Content-Disposition: attachment; filename="download.php"); 
print $content; 
+0

你可以在'wrap'中定義'...如何在頁面上包裝PHP函數和代碼? – Jelmer

+0

嗨,因爲我必須打印$內容;獲取頁面上的特定部分。所以,據我所知是$ content =「我想要打印的內容」。但我不知道如何包裝php:$ content =「<?php function get_text($ text){....」; – richard

+1

只需轉義將其作爲php進行處理的字符即可。例如,當你在一個HTML文件中展示東西時,你可以從每個'''''<'開始。對於這種類型的東西,你可以使用['html_entity_decode'](http://php.net/manual/en/function.html-entity-decode.php)等函數。你只需要弄清楚你使用的是什麼字符集以及你如何呈現它。 – Jelmer

回答

1

不知道這是喲,但你可以創建一個調用的第一個,得到的輸出,併發送與所提到的頭第二個腳本:

<?php 
$words = (int) $_GET['words']; 
$sort = $_GET['sort']; 

$url = sprintf("http://localhost/wherever/download.php?words=%d&sort=%s&type=xml", $words, $sort); 
$content = file_get_contents($url); 

header("Content-Disposition: attachment; filename="download.php"); 
print $content; 
?> 

調用文件「force_download.php」,讓用戶撥打<a href="force_download.php?words=2000&sort=popular&type=xml">Download php file</a>代替。