CodeCharge Studio
search Register Login  

Visual Web Reporting

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> PHP

 [Solved] How to save php output to static file

Print topic Send  topic

Author Message
Markie


Posts: 251
Posted: 01/28/2009, 1:41 PM

I want to save the output of a CCS php page (including layout) to a static file for archiving purposes. I wonder if somebody has done this before. Is it only necessary to add:

<?php
ob_start();

at the top of the page, and

$page = ob_get_contents();
ob_end_flush();
$fp = fopen("output.html","w");
fwrite($fp,$page);
fclose($fp);
?>

at the bottom of the page, or do I have to do something else ?
_________________
The Netherlands, GMT+1
Tools: CCS 5.1, Windows 7, Navicat, Ultraedit
Local server: XAMPP with Apache, php and MySQL
Webserver: Windows 2008 IIS 7, php and MySQL
View profile  Send private message
Gena

Posts: 591
Posted: 01/28/2009, 2:11 PM

it's quite easy. Like this
  
$url = ServerURL . 'MyCreatePage.php?nl_date_from=' . $nl_date_from . '&nl_date_to=' . $nl_date_to . '&nl_id=' .$nl_id;  
// now $url consists of your page like it is normal page which you open in web browser, with parametes etc  
  
$contents = file_get_contents($url); // you have all HTML page contents now!  
  
// here you can do with it what you want!  
  

_________________
Gena
View profile  Send private message
Gena

Posts: 591
Posted: 01/28/2009, 2:17 PM

you can evere save it into DB (like me) so after you can show this fields as Label (HTML) and it will show your historical page! :-)
_________________
Gena
View profile  Send private message
Markie


Posts: 251
Posted: 01/30/2009, 8:48 AM

Hi Gena, your solution is kind of working. I use this code:

$url = ServerURL . 'mypage.php';
$contents = file_get_contents($url);
$fp = fopen("test/output.html","w");
fwrite($fp,$contents);
fclose($fp);

I want the output to be html and it works, the html file is saved in the test folder.
But, when I look at the file, I can only see the code as I have made in CCS , and not the real dynamic HTML code like the source of the page in the browser.
_________________
The Netherlands, GMT+1
Tools: CCS 5.1, Windows 7, Navicat, Ultraedit
Local server: XAMPP with Apache, php and MySQL
Webserver: Windows 2008 IIS 7, php and MySQL
View profile  Send private message
Gena

Posts: 591
Posted: 01/30/2009, 9:05 AM

not sure I understand what is "not the real dynamic HTML code"... When you "catch" your Dinamic page and then save it - it becomes Static HTML page, isn't it?

Please clarify your problem.
_________________
Gena
View profile  Send private message
Gena

Posts: 591
Posted: 01/30/2009, 9:08 AM

Since you move your html files in separate folder (test/) may be you miss style CSS files and related images? Because PATH (from your saved html files) is DIFFERENT now...
_________________
Gena
View profile  Send private message
Markie


Posts: 251
Posted: 01/31/2009, 1:44 AM

Hi Gena, what I meant with "not the real dynamic HTML code"... is this:
when I look at the page in my webbrowser, I see photos.
when I look at the output of the static html page, I don't see any pointers to images (like <img src ...">) but only "no photos found", which is what I have put in the CCS code.

Do you have any idea why this is ?
_________________
The Netherlands, GMT+1
Tools: CCS 5.1, Windows 7, Navicat, Ultraedit
Local server: XAMPP with Apache, php and MySQL
Webserver: Windows 2008 IIS 7, php and MySQL
View profile  Send private message
damian

Posts: 838
Posted: 01/31/2009, 2:35 AM

try this way....

$contents = implode("", file("http://fqdn/pagename.php?variable=".$Component->GetValue()));


_________________
if you found this post useful take the time to help someone else.... :)
View profile  Send private message
Gena

Posts: 591
Posted: 01/31/2009, 4:00 AM

As I have already wrote, since you move your html files in separate folder (test/) may be you miss style CSS files and related images if you use RELATIVE PATH? Because PATH (from place your saved html files) is DIFFERENT now...

In this case you just need to save it with FULL path...

_________________
Gena
View profile  Send private message
Markie


Posts: 251
Posted: 01/31/2009, 6:31 AM

Hi Gena, I understand you. However, it doesn't work well for me at the moment. I will take a further look at curl http://nl3.php.net/curl to see if that does the trick.

Thanks !
_________________
The Netherlands, GMT+1
Tools: CCS 5.1, Windows 7, Navicat, Ultraedit
Local server: XAMPP with Apache, php and MySQL
Webserver: Windows 2008 IIS 7, php and MySQL
View profile  Send private message
damian

Posts: 838
Posted: 01/31/2009, 11:49 PM

hi Markie - did you try using implode() instead?
also what Gena is saying is you may need to define the full path for those elements that are not displaying eg src="http://fqdn/images/{myimage}"
_________________
if you found this post useful take the time to help someone else.... :)
View profile  Send private message
jjrjr1


Posts: 942
Posted: 02/01/2009, 7:23 AM

Hi Markie.

If I understand your question properly, The code below will write the output of a CCS page to a file. It will be the actual HTML that displays on the clients browser including CSS style etc.

In the before output event add this custom code:

global $main_block;

$save_block = $main_block;

$fp = fopen("output.html","w");
fwrite($fp,$save_block);
fclose($fp);

This will put the entire HTML content for the CCS page into your file output.html.

Remember that this is the actual HTML so urls inside this HTML might not have the absolute paths to images or other included files in the HTML.

eg: in the HTML an image might look like this <img src="images/pic.jpg />

This is what will appear in the saved HTML. So when viewed locally the path might not resolve images or scripts or CSS sheets... etc.

You can try fixing that using:

$URL='src="http://thedomain.com/';

$save_block=str_replace('src="',$URL,$main_block);

Or something similar to that depending on the CCS original file


NOTE: The string $main_block contains the output of any CCS page. It is analagous to using the php output buffer "eg: ob_start()". If manipulated in the before output event you have the ability to do anything you want with the final output of any CCS page. Other uses is this being a good place to capture a page to convert to PDF... etc... Remember, any changes made to $main_block will actually change the HTML output of the CCS page. Knowing this, another example might be, $main_block=WhatEverPdfConverionRoutineYouUse($main_block); would turn your page into a PDF. (Must Change headers too).

BTW, this is one reason I use DOMPDF since it will process an existing HTLM string. The security problem with DOMPDF is easily fixed by removing the ability to execute from an HTTP request and only be called as a php cgi called from your CCS application. Also changing the name of the executable removes the security issue as associated with CCS applications. For me, anyway, this works great since the PDF display will be exacty what the CCS page generated was, making creation of a PDF as easy as creating a CCS page.

Another example, you will also see an implementation of this technique in CCVideoServer. The CCVIdeoplayer.php file uses this ability to switch in the proper player based on what media type is to be rendered.


Incidentally, CCS has an action in the before output event called "Convert to PDF". The only problem with this is that it is for windows servers using Yes's conversion code. For Linux you need to use something else. In my case I modified the convert to PDF action to use DOMPDF instead.
Hope that helps

Have fun.

_________________
John Real - More CodeCharge Studio Support at - http://CCSElite.com
View profile  Send private message
Markie


Posts: 251
Posted: 02/01/2009, 1:18 PM

Wow John, what a simple solution. It does exactly what I wanted to achieve.

@Gena and Damian: again a lot of thanks !
_________________
The Netherlands, GMT+1
Tools: CCS 5.1, Windows 7, Navicat, Ultraedit
Local server: XAMPP with Apache, php and MySQL
Webserver: Windows 2008 IIS 7, php and MySQL
View profile  Send private message
jjrjr1


Posts: 942
Posted: 02/02/2009, 5:48 PM

Markie,

Really glad that helped.


_________________
John Real - More CodeCharge Studio Support at - http://CCSElite.com
View profile  Send private message
DonP
Posted: 02/04/2009, 10:17 AM

This may be overkill for what you are trying to do but in case you want
the HTML versions to be plain and have their own footers, headers or
other changes, the code below is what I use for my Print Page version.
In this case it saves the output to a table (which it then presents in a
pop up for printing) but it can just as easily create an HTML document
too with slight modifications.

I have it stripping out all forms, links (leaving the text that was
linked), JavaScript, backgrounds etc., and then the print_page.php file
puts in different headers and footers that are appropriate for printing,
which are simply done in the HTML template. To make it grab the portion
of the page you want, you'll need to add a pair of remarks to the page,
<!--print--> at the beginning and end of the portion you need. On my
page, the first is after the BODY tag and after the DHTML menu, and
another before the included footer page gets loaded.

Each page visited saves the sessions with the document info to sessions
using a Before Show event. I did it this way because my pages are
somewhat convoluted with lots of lookups and this seemed the best way to
get what I needed. It does add to the overhead, though, so the general
user can likely do it differently:

(Sorry, some of these lines are long and may have wrapped oddly):

	global $FileName;  
  
if ($FileName == "mainpages.php") {  
	$ServerName = $_SERVER['HTTP_HOST'];  
	$ScriptName = $_SERVER['SCRIPT_NAME'];  
  
	CCSetSession("PrintVersionPath", $ServerName . $ScriptName .   
$QueryString);  
	CCSetSession("PrintVersionParam", $QueryString);  
	$PrintButton = "<img   
src=\"/php/show_button.php?Button=14&Text=Print+Page\" alt=\"Open   
printable version of this page\" border=\"0\">";  
	$PrintableVersion->SetValue("<a href=\"/php/print_page.php\"   
onClick=\"newWindow('600', '400', 'yes', 'PrintWindow');\"   
class=\"PrintButton\" target=\"PrintWindow\">" . $PrintButton . "</a>");  
} else {  
	$PrintableVersion->SetValue("");  
}

Then a separate page also in a Before Show:

$PageCode = getFile(CCGetSession("PrintVersionPath"),   
CCGetSession("PrintVersionParam"));  
  
if ($PageCode) {  
	$pieces = explode("<!--print-->", $PageCode);  
	$PageCode = $pieces[1]; // Main page body, between <!--print--> tags  
  
	// Strip all links but leave link text  
	$PageCode = eregi_replace('(<a [^<]*href=["|\']?([^   
"\']*)["|\']?[^>]*>)','', $PageCode);  
	$PageCode = str_replace("</a>", "", $PageCode);  
  
	// Set tables to full width  
	$PageCode = str_replace("80\%", "100\%", $PageCode);  
  
	// Strip all scripts  
	$PageCode = eregi_replace('<script("|\')?([^   
"\']*)("|\')?.*>([^<]*)</script>', '', $PageCode);  
  
	// Strip all remarks  
	$PageCode = eregi_replace("<!--([^-]*([^-]|-([^-]|-[^>])))*-->", "",   
$PageCode);  
  
	// Strip background image  
	$PageCode = str_replace("background=\"/images/background.jpg\"", "",   
$PageCode);  
  
	// Strip all forms  
	$PageCode = eregi_replace('<form("|\')?([^   
"\']*)("|\')?.*>([^<]*)</form>', '', $PageCode);  
  
	$DBdatabase = new clsDBdatabase();  
	// Create a temp table using a custom random character function as   
name, drop when finished  
	$TableName = randchr(15);  
  
	$SQL1 = "CREATE TABLE " . $TableName . " (`PageContents` TEXT)";  
	$SQL2 = "INSERT INTO " . $TableName . " (PageContents) ".  
			"VALUES (". CCToSQL($PageCode,ccsText) .")";  
	$SQL3 = "DROP TABLE `" . $TableName . "`";  
  
	// For testing purposes only  
	CCSetSession("TableName", $TableName);  
  
	$DBdatabase->query($SQL1);  
	$DBdatabase->query($SQL2);  
  
	$PageHTML->SetValue(CCDLookup("PageContents", $TableName, "PageContents   
IS NOT NULL", $DBdatabase));  
  
	$DBdatabase->query($SQL3);  
	$DBdatabase->close();  
}

Don (DonP)

Markie wrote:
> I want to save the output of a CCS php page (including layout) to a static file
> for archiving purposes. I wonder if somebody has done this before. Is it only
> necessary to add:
>
> <?php
> ob_start();
>
> at the top of the page, and
>
> $page = ob_get_contents();
> ob_end_flush();
> $fp = fopen("output.html","w");
> fwrite($fp,$page);
> fclose($fp);
> ?>
>
> at the bottom of the page, or do I have to do something else ?
> _________________
> The Netherlands, GMT+1
> Tools: CCS 4.1.00.027, Win XP, Navicat, PSPad
> Local server: XAMPP with Apache, php and MySQL
> Webserver: Ubuntu with Apache, php and MySQL
> ---------------------------------------
> Sent from YesSoftware forum
> http://forums.yessoftware.com/
>

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

Internet Database

Visually create Web enabled database applications in minutes.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.