Warptweet Posted January 12, 2007 Share Posted January 12, 2007 I got an answer before, but was very vague and didn't exactly answer the question...Is it possible to write HTML or PHP to a file?If so, can you please explain how?Thanks,-Warptweet Link to comment https://forums.phpfreaks.com/topic/33853-writing-html-or-php-to-a-file/ Share on other sites More sharing options...
Jessica Posted January 12, 2007 Share Posted January 12, 2007 Exactly the same as you would write text to a file. Save the file as .php instead of .txt, and make sure to use your <?php ?> tags. Link to comment https://forums.phpfreaks.com/topic/33853-writing-html-or-php-to-a-file/#findComment-158863 Share on other sites More sharing options...
zfred09 Posted January 12, 2007 Share Posted January 12, 2007 try thisob_start();?><---make sure you close your php here<html></html><?phpchdir("directory");$fp = fopen("$filename.php", "w") ;fwrite($fp, ob_get_contents());fclose($fp);ob_end_flush(); Link to comment https://forums.phpfreaks.com/topic/33853-writing-html-or-php-to-a-file/#findComment-158866 Share on other sites More sharing options...
scotmcc Posted January 12, 2007 Share Posted January 12, 2007 To write data to a file:<?php$text = "<p>Write this to a file.</p>"; $file = "myfile.html"; if (!$my_file = fopen($file,"a")) { echo "Unable to open $file"; } if (!fwrite($my_file, $text)) { echo "Unable to write to $file"; } echo "Text entered into $file"; fclose($my_file); ?> Scot McConnaughay Link to comment https://forums.phpfreaks.com/topic/33853-writing-html-or-php-to-a-file/#findComment-158868 Share on other sites More sharing options...
Warptweet Posted January 12, 2007 Author Share Posted January 12, 2007 Okay...Well, one of you says to do it as I would any other PHP... Except save it as a .php file when I create the file..Now, two of you are saying two different methods, this is seriously confusing. Can you please tell me the sure fire way? Link to comment https://forums.phpfreaks.com/topic/33853-writing-html-or-php-to-a-file/#findComment-158871 Share on other sites More sharing options...
Jessica Posted January 12, 2007 Share Posted January 12, 2007 How about you try them both and find out. I would put all the PHP code in a big string, and write it to a file. But because I don't really care, I haven't tried it. You care. Try it.I didn't say write it as php, I said as any file. You'd put a string of text in a file.$str = "Text".Do $str "<?php print 'hello world';?> ";Does it work? I don't know. But it won't kill you to try since you want to do it. Link to comment https://forums.phpfreaks.com/topic/33853-writing-html-or-php-to-a-file/#findComment-158880 Share on other sites More sharing options...
scotmcc Posted January 12, 2007 Share Posted January 12, 2007 My code seems to work on my server :)Scot Link to comment https://forums.phpfreaks.com/topic/33853-writing-html-or-php-to-a-file/#findComment-158887 Share on other sites More sharing options...
Warptweet Posted January 12, 2007 Author Share Posted January 12, 2007 I presumed your code would work, although I have not tried it yet.I'm busy doing the uploader part, I'll try soon. Link to comment https://forums.phpfreaks.com/topic/33853-writing-html-or-php-to-a-file/#findComment-158907 Share on other sites More sharing options...
Guest Posted February 15, 2007 Share Posted February 15, 2007 I've done this before. Jesirose's suggestion to do (as quoted)$str = "<?php print 'hello world'; ?>";will work. I've used this method to create cleanup scripts on-the-fly to unlink temp files and unnecessary scripts useless to the application after a script installation finishes. Link to comment https://forums.phpfreaks.com/topic/33853-writing-html-or-php-to-a-file/#findComment-185148 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.