Doqrs Posted February 1, 2007 Share Posted February 1, 2007 Hello I am looking to use fwrite to write a php script just as a counter opens up a .txt file. $filename = "dir/" . $time . ".php"; $filewrite = -My PHP script here with <? stuff ?>- $fp = fopen($filename, "w+"); fwrite($fp, $filewrite); fclose($fp); Link to comment https://forums.phpfreaks.com/topic/36707-solved-write-a-php-file-with-php/ Share on other sites More sharing options...
Daniel0 Posted February 1, 2007 Share Posted February 1, 2007 <?php $contents = <<<EOF all the contents in here EOF; $fp = fopen("dir/{$time}.php", "w+"); fwrite($fp, $contents); fclose($fp); ?> Link to comment https://forums.phpfreaks.com/topic/36707-solved-write-a-php-file-with-php/#findComment-175020 Share on other sites More sharing options...
Doqrs Posted February 1, 2007 Author Share Posted February 1, 2007 thank you how would i be able to get the <<<EOF heredoc stuff to work if i also want to have actual php code and not just text? I wasn't able to get it to work with this: <?php $contents = <<<EOF header("location: somewhere"); EOF; $fp = fopen("dir/{$time}.php", "w+"); fwrite($fp, $contents); fclose($fp); ?> Link to comment https://forums.phpfreaks.com/topic/36707-solved-write-a-php-file-with-php/#findComment-175023 Share on other sites More sharing options...
Daniel0 Posted February 1, 2007 Share Posted February 1, 2007 The code snippet you posted above works fine for me (also with the opening and closing PHP tags). Link to comment https://forums.phpfreaks.com/topic/36707-solved-write-a-php-file-with-php/#findComment-175026 Share on other sites More sharing options...
janroald Posted February 1, 2007 Share Posted February 1, 2007 Yep, that code snippet should work fine, as Doqrs says. Maybe you have some indentation in your real code that makes it fail, like : if(smthing) { $contents = <<<EOF header("location: somewhere"); EOF; } In this case, you will get an error, because you cant have spaces or tabs or anything other than "EOF;" on the EOF line. If it's your fopen/fwrite that fails you should check file/folder permissions. http://no2.php.net/manual/en/function.fopen.php http://no2.php.net/manual/en/function.fwrite.php Link to comment https://forums.phpfreaks.com/topic/36707-solved-write-a-php-file-with-php/#findComment-175041 Share on other sites More sharing options...
Doqrs Posted February 1, 2007 Author Share Posted February 1, 2007 thanks a lot, I had the ending heredoc bracket tabbed over and forgot it had to be aligned to the left Link to comment https://forums.phpfreaks.com/topic/36707-solved-write-a-php-file-with-php/#findComment-175042 Share on other sites More sharing options...
janroald Posted February 1, 2007 Share Posted February 1, 2007 Hehe, you replied before i modified my post, and obviously the added info wasn't needed :-) The "left-align" thingy with heredoc is really annoying. Breaks indentation style. i wish for a perl-like qw structure in php :-) Link to comment https://forums.phpfreaks.com/topic/36707-solved-write-a-php-file-with-php/#findComment-175047 Share on other sites More sharing options...
Daniel0 Posted February 1, 2007 Share Posted February 1, 2007 It simply just have to be the only thing at that line. Link to comment https://forums.phpfreaks.com/topic/36707-solved-write-a-php-file-with-php/#findComment-175051 Share on other sites More sharing options...
janroald Posted February 1, 2007 Share Posted February 1, 2007 It simply just have to be the only thing at that line. Are you just repeating everything i say in all threads now? ;-) Link to comment https://forums.phpfreaks.com/topic/36707-solved-write-a-php-file-with-php/#findComment-175053 Share on other sites More sharing options...
Daniel0 Posted February 1, 2007 Share Posted February 1, 2007 Ah sorry, sometimes I just skip to the last post... Maybe I should start reading the entire topic Link to comment https://forums.phpfreaks.com/topic/36707-solved-write-a-php-file-with-php/#findComment-175060 Share on other sites More sharing options...
janroald Posted February 1, 2007 Share Posted February 1, 2007 Thought you were just trying to get to 1500 posts before midnight, lol :-P Link to comment https://forums.phpfreaks.com/topic/36707-solved-write-a-php-file-with-php/#findComment-175064 Share on other sites More sharing options...
Daniel0 Posted February 1, 2007 Share Posted February 1, 2007 That'd be too late. It's 00:07 here. Link to comment https://forums.phpfreaks.com/topic/36707-solved-write-a-php-file-with-php/#findComment-175065 Share on other sites More sharing options...
janroald Posted February 1, 2007 Share Posted February 1, 2007 Here too :-) but you still had a couple of minutes when you wrote your solution :-) Link to comment https://forums.phpfreaks.com/topic/36707-solved-write-a-php-file-with-php/#findComment-175070 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.