Hello all!
I am making a web application that allows someone to create a website through the various steps of the app. The system is based on templates, that a user picks and then chooses the content to go into it. I have all the info stored in a session variable and every template uses this session variable to put the content in.
I need the ability to save these pages as HTML files so that it can be uploaded to a server and used as a site. The problem is when I try to copy the file, it keeps the PHP in the file - and does not execute the php. I need to know how to execute a php file and then get the content of it so that I can store it into another file as just HTML.

How would I best go about doing this?

Hi kroche!

As simple as that:

<?
$f=file("http://localhost/test/test.php");
for($x=0;$x<count($f);$x++){
	echo $f[$x];
}
?>

Whereas "test.php" is your PHP file that you want to save. And instead of doing an echo, you simply open a file for writing and put all that content in it. VoilĂ .

Hi kroche!

As simple as that:

<?
$f=file("http://localhost/test/test.php");
for($x=0;$x<count($f);$x++){
	echo $f[$x];
}
?>

Whereas "test.php" is your PHP file that you want to save. And instead of doing an echo, you simply open a file for writing and put all that content in it. VoilĂ .

I don't fully understand what your code is doing - so I am going to try to explain what I need better and then if your code works for what I need, then I will need an explanation. :$

I can better explain in an example.

Example Template: template.php

<?php $id = $_GET['id']; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title><?php echo $_SESSION['pages'][$id]['title']; ?></title>
	</head>
	<body>
            <?php echo $_SESSION['pages'][$id]['content']; ?>
	</body>
</html>

That is a very basic example of what a template would look like.

$_SESSION variable set-up:

$_SESSION['pages'][$i]['name'] = "filename.html"
                      ['title'] = "Page Title"
		      ['heading'] = "Heading for page"
		      ['content'] = "Page Content"

I would like to be able to click a link that provides the page ID to the appropriate template, and then the page is compiled and saved to a specified path.

I have tried to accomplish this on my own with a combo of javascript and a php script.

See below:
Example Link: (PHP Generated for each page - rel is ID of page)

<a rel="2" class="pageSaveButton" target="fileuploadframe" href="template.php?pg=2">Page Name</a>

JS Function

$(".pageSaveButton").bind("click",function(){
	var theID = $(this).attr("rel");
	$("#fileuploadframe").load(function(){
		var response = $("#fileuploadframe").contents().find("html").html();
		$.post(/*Script File Here*/,{action:"savePage",html:response, id: theID}, function(data){
			alert(data);
		});
	});
});

PHP Script

$page = fopen(/* Path to save file to */,'w');
fwrite($page, "<html>");
fwrite($page, stripslashes($_POST['html']));
fwrite($page, "</html>");
fclose($page);

The only problem I have had with my implementation is that if I click to save a file (pg1) - it works, but then if I click to save another one (pg2), it saves the pg2 file and then saves over the pg1 file with the content of pg2 - it also does this if there are multiple previous pages. I just have not had the chance to try and track where the error is occurring in the code.

Sorry for my long-winded response :), but I thought I should try to explain as thoroughly as possible. Let me know if I could explain better - and thanks for your help!

Hi kroche,

why are you doing it so difficultly? To create a file out of the output of PHP, you just can access the file via http://. It even works with copy(), if just tried out.

So, for instance. You have your layout-page that includes some kind of data given by the user:

<html>
<head>
<title><?php include("./tmp/title".$_REQUEST['id'].".txt");?></title>
</head>
<body><!--some very cool top layouts.--!>
<?php
include("./tmp/header".$_REQUEST['id'].".txt");  //lets assume you saved the user's 
include("./tmp/content".$_REQUEST['id'].".txt");  //lets assume you saved the user's input here
?>
</body</html>

Now, when you use copy(), PHP first executes the script and returns the processed text. Simply pass the id to the script.

<?php
copy("http://localhost/layout.php?id=".$_GET['id'],"./online/".$_SESSION['pages'][$id]['content']);
?>

I haven't tested it, but I'm quite confident, that this should basically work.

The only thing that you have to do is to save the data in the setup to files instead of a session. If you like you could also work with sessions but then you have to pass all arguments to the script when using copy.

Hope that helps you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.