使用fwrite将PHP代码保存到文件中

时间:2011-09-23 19:13:46

标签: php facebook-php-sdk 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 />

所以我想我真正要问的是如何使用带有.php扩展名的fwrite保存文件,并在保存的文件中包含一个php脚本。这是我试图添加到使用fwrite(Facebook的PHP SDK)保存的页面的PHP脚本的示例:

<?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>";
?>

2 个答案:

答案 0 :(得分:2)

好的,所以你的问题是你不能以这种方式引用php文件,因为服务器会解析它。有一个单独的脚本来设置标题并使其成为链接的目标。

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

答案 1 :(得分:1)

好的,既然您已经更新了问题,那么解决方案非常明确:

当您执行链接所说的内容时,您会得到这些结果,对吗?你右键单击该链接并执行“另存为...” - 但你并没有真正保存php代码,而是服务器输出的内容,因为php代码首先由服务器执行。

相关问题