2011-09-23 52 views
0

我有一個腳本我工作的一個小問題,這就是我遇到:保存PHP代碼文件中使用的fwrite

首先一點關於如何腳本作品。它實際上使用WordPress(它是一個插件),它根據可在後端更改的各種設置生成動態頁面。我將添加一個導出到HTML功能,他們可以在插件中創建頁面後創建該頁面的靜態版本。

在這個導出的頁面中,我需要它將一個PHP函數保存在文件的頂部,另一個保存在頁面的其他地方。這就是我想要做的事:

$fbscript=file_get_contents('fbscript.txt'); 
$newcontent=str_replace('<!-- dont erase this line -->',$fbscript,$newcontent); 
$fbscript2=file_get_contents('fbscript2.txt'); 
$newcontent=str_replace('<!-- dont erase this line 2 -->',$fbscript2,$newcontent); 

的2個dont erase this line東西都在它需要把腳本動態頁面的某個地方。這就是某處出在出口上一條:

<br /> 
<b>Notice</b>: Undefined index: signed_request in <b>C:\xampp\htdocs\wp\wp-content\plugins\easyfanpagedesign\default.theme\htmlpost-31345.php</b> on line <b>82</b><br /> 
<br /> 
<b>Notice</b>: Undefined offset: 1 in <b>C:\xampp\htdocs\wp\wp-content\plugins\easyfanpagedesign\default.theme\htmlpost-31345.php</b> on line <b>3</b><br /> 

所以我想我真正想要問的是我如何使用fwrite的一個.php擴展名保存文件,並有內部的一個PHP腳本保存的文件。這是PHP腳本我想添加到頁面使用FWRITE(Facebook的PHP SDK)保存的例子:

<?php 
function parsePageSignedRequesttt($signed_request, $secret) { 
    list($encoded_sig, $payload) = explode('.', $signed_request, 2); 
    $sig = base64_url_decode($encoded_sig); 
    $data = json_decode(base64_url_decode($payload), true); 
    if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') { 
    return null; 
    } 
    $expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true); 
    if ($sig !== $expected_sig) { 
    return null; 
    } 
    return $data; 
} 
function base64_url_decode($input) { 
    return base64_decode(strtr($input, '-_', '+/')); 
} 
?> 

這是我該做的一切整個export.php文件:

<?php 
$content=$_POST['efpd_page_content']; 
$uistyle=$_POST['efpd_ui_style']; 
$app_id=$_POST['efpd_appid']; 
$the_fscripts=$_POST['efpd_fscripts']; 
$the_hscripts=$_POST['efpd_hscripts']; 
$bgstuff=$_POST['efpd_bgstuff']; 
$jquery=$_POST['efpd_jquery']; 
$jqueryui=$_POST['efpd_jqueryui']; 
$cycle=$_POST['efpd_cycle']; 
$copytext=$_POST['efpd_copytext']; 
$affstr=$_POST['efpd_affstr']; 
$the_style=$_POST['efpd_style']; 
$the_gwf=$_POST['efpd_gwfstyle']; 
$secret=$_POST['efpd_secret']; 

if(empty($secret)){$secret=999999;} 

$newcontent=file_get_contents($_POST['efpd_refurl']); 

$fbscript=file_get_contents('fbscript.txt'); 
$newcontent=str_replace('<!-- dont erase this line -->',$fbscript,$newcontent); 
$fbscript2=file_get_contents('fbscript2.txt'); 
$newcontent=str_replace('<!-- dont erase this line 2 -->',$fbscript2,$newcontent); 
$newcontent=str_replace('THE_SECRET',$secret,$newcontent); 

//die(var_dump($newcontent)); 

$int=rand(1,99999); 
$savefile = "htmlpost-$int.php"; 
$handle = fopen($savefile, 'w') or die("can't open file"); 
fwrite($handle,$newcontent); 
fclose($handle); 
echo "<a href='$savefile'>Right Click &gt; Save As to download the generated HTML page.</a>"; 
?> 
+0

代碼中的某處必須是'$ _REQUEST ['signed_request']',對不對? – Niko

+0

是的,這是在頁面中間添加的第二個腳本(fbscript2.txt) – Jared

+0

好吧,這就是導致這些錯誤出現的原因 - 因爲該頁面似乎旨在成爲Facebook上的應用程序和你不要真的在那裏運行它(現在呢?)。 – Niko

回答

2

好吧,所以你的問題是你不能以這種方式引用一個PHP文件,因爲服務器會解析它。有一個單獨的腳本來設置標題並將其作爲鏈接的目標。

header('Content-type: text/plain'); 
header('Content-Disposition: attachment; filename="filename.php"'); 
readfile('filename.php'); 
exit; 
+0

您可以完全放棄鏈接,只需讓我提供的代碼出現在腳本的末尾即可。您必須確保您不會輸出任何內容,以便直到最後纔會發送標題。 – gview

+0

這是在fwrite之前還是在其他地方? – Jared

+0

不,這將是你寫和關閉文件後。 readfile將再次讀取它並將內容返回到緩衝區。用我的filename.php替換你的$ savefile變量。 – gview

1

好了,現在你已經更新了你的問題的解決是相當清楚的:

你得到這些結果時,你做你的鏈接說什麼,對不對?您右鍵單擊該鏈接並執行「S​​ave as ..」 - 但您並不真正保存php代碼,而只是服務器輸出的內容,因爲php代碼首先被服務器執行。

+0

是的,但我其實只是試圖讓它保存PHP代碼,而不是執行它。 – Jared

+0

PHP代碼位於文件中,但您無法以這種方式將其保存到本地驅動器。請參閱gview的答案以獲得解決該問題的一些有用的代碼! – Niko