Save PHP code to file using fwrite
I'm having a little problem with a script I'm working on, and this is what I'm running into:
First of all a bit about how the script works. It actually uses WordPress (it's a plugin) and it makes dynamic pages based on various settings they can change in the backend. I'm adding an Export to HTML feature where they can make a static version of that page after they have already created the page in the plugin.
In this exported page I need it to save a PHP function at the top of the file, and another one somewhere else in the page. This is what I'开发者_开发问答m trying to do:
$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);
The 2 dont erase this line
things are somewhere in the dynamic pages where it needs to put the scripts. This is what is showing up somewhere in the exported page:
<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 />
So I guess what I'm really trying to ask is how can I save a file using fwrite with a .php
extension and have a php script inside the saved file. This is an example of the php script I'm trying to add to the page saved using fwrite (Facebook's 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, '-_', '+/'));
}
?>
This is my entire export.php file which does everything:
<?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 > Save As to download the generated HTML page.</a>";
?>
Ok, so your problem is that you can not reference a php file in that way, because the server will parse it. Have a seperate script that sets the header and make this the target of your link.
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="filename.php"');
readfile('filename.php');
exit;
Okay, now that you've updated your question the solution is pretty clear:
You get those results when you do what your link says, right? You right-click on that link and do "Save as.." - but you don't really save the php code but just what the server outputs as the php code first gets executed by the server.
精彩评论