visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: PHP :: Saving PHP page to HTML page, How?


By: excinc U.S.A.  Date: 06/10/2003 00:00:00  English  Points: 50 Status: Answered
Quality : Excellent
Hi!

I have php-page which prints data to page, so how can I save the printed html page to html file in some folder without asking anything from the user?

So I should add some code in to end of the php file which saves the page to html-page in some folder.
How to do that?



Regards,
excinc
By: VGR Date: 06/10/2003 16:41:00 English  Type : Comment
ob_start() before outputting anything
// here HTML output created by normal PHP code
$contents=ob_flush_ends(); when it's finished
//then write to file
$fd=@fopen('thefile.html','w');
if ($fd) { fputs($fd,$contents); fclose($fd); } // else NOP
// then send to user's browser :
echo $contents;
By: excinc Date: 06/10/2003 16:45:00 English  Type : Comment
And it should create the folder which the file will be saved like:

c:\2003\09\07\file.html
By: excinc Date: 06/10/2003 16:57:00 English  Type : Comment
Parse error: parse error, unexpected T_VARIABLE in c:\test.php on line 50
So the line is: $fd=@fopen('thefile.html','w');
By: VGR Date: 07/10/2003 05:36:00 English  Type : Comment
no, it's the line before probably

I hope you haven't used copy-pasted the raw code above, 'cause there are comments in it...
By: VGR Date: 07/10/2003 05:37:00 English  Type : Comment
for your windows path, either you use normal forward slashes, or you escape (double) the backwards slashes
By: excinc Date: 07/10/2003 05:43:00 English  Type : Comment
Hmm... yeah... I didn't just copy paste it... forget one ";"... usual mistake for newbie :P

But now: Fatal error: Call to undefined function: ob_flush_ends() in
By: excinc Date: 07/10/2003 05:46:00 English  Type : Comment
And it won't create the file? It should create it?

But when I create empty thefile.html first it prints this to the file: RIFF2WAVEfmt "V"V(factdata
By: excinc Date: 07/10/2003 05:49:00 English  Type : Comment
that fopen just opens the file...
I need to php create the file...
when I print data to html page with php I for example need to create these _files and folders_:

c:\test\2003\09\07\12_00.html
c:\test\2003\09\08\13_00.html
c:\test\2003\10\07\14_43.html
c:\test\2003\10\07\13_23.html



By: VGR Date: 07/10/2003 18:51:00 English  Type : Answer
yes, you've to make some efforts too 8-))

like looking the "ob_* functions in the online Manual)

the function is ob_end_flush, not ob_flush_ends

8-))

this said, here's what you must do :

ob_start(); // before outputting anything
// here HTML output created by normal PHP code
$contents=ob_get_contents(); //when it's finished
ob_end_clean(); // stop output buffering
//then write to file
$fd=@fopen('thefile.html','w'); // this REWRITES (creates) the file
if ($fd) { fputs($fd,$contents); fclose($fd); } // else NOP
// then send to user's browser :
echo $contents;
By: VGR Date: 07/10/2003 18:56:00 English  Type : Comment
and if your problem is for PHP to create the directory structure (tree) between c:\test and the final file '12_00.html', then sorry ther's no ready-made solution.

do it like this :
$zarma=explode($path,'/'); // or the reverse for arguments, I'm not perfect and pHP is unconsistent
$current="/";
for ($i=0;$i<count($zarma)-1;$i++) { // we don't want the last one = filename
$dirto=$zarma[$i];
if (!file_exists($current.$dirto)) { // directory doesn't exist
if (!mkdir($current.$dirto)) die('could not create directory $current.$dirto');
}
} // foreach


or the like
By: VGR Date: 07/10/2003 18:57:00 English  Type : Comment
trivial correction :

$zarma=explode($path,'/'); // or the reverse for arguments, I'm not perfect and pHP is unconsistent
$current="/";
for ($i=0;$i<count($zarma)-1;$i++) { // we don't want the last one = filename
$dirto=$zarma[$i];
if (!file_exists($current.$dirto)) { // directory doesn't exist
if (!mkdir($current.$dirto)) die('could not create directory $current.$dirto');
}
$current=$current.$dirto.'/';
} // foreach
By: excinc Date: 07/10/2003 21:02:00 English  Type : Comment
Hmm... How do you combine those codes?
By: excinc Date: 07/10/2003 21:51:00 English  Type : Comment
Okay thanks... got work a little different way...

Do register to be able to answer

EContact
browser fav
page generated in 367.345810 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page