zfred09 Posted January 12, 2007 Share Posted January 12, 2007 Alright say that when a condition is met, an entirely new php file is created with fwrite() and there is no code in it. How do you automatically write php code into that file using a script? Link to comment https://forums.phpfreaks.com/topic/33958-fwrite-and-php/ Share on other sites More sharing options...
trq Posted January 12, 2007 Share Posted January 12, 2007 [url=http://php.net/fwrite]fwrite[/url](). Link to comment https://forums.phpfreaks.com/topic/33958-fwrite-and-php/#findComment-159483 Share on other sites More sharing options...
zfred09 Posted January 12, 2007 Author Share Posted January 12, 2007 Yes I know fwrite writes to a file, maybe my question wasn't clear, how do I write php code to that new file, because this method below doesn't work with php, you get unexpected string errors.[code]$text = "<?php echo("hi dude"); $_SESSION['hello']="Hi"; ?>"; $file = "aaa.php"; 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); [/code] Link to comment https://forums.phpfreaks.com/topic/33958-fwrite-and-php/#findComment-159489 Share on other sites More sharing options...
ShogunWarrior Posted January 12, 2007 Share Posted January 12, 2007 Just remember you need to escape quotes inside a string or use single quotes so this should work:$text = '<?php echo("hi dude"); $_SESSION["hello"]="Hi"; ?>'; Link to comment https://forums.phpfreaks.com/topic/33958-fwrite-and-php/#findComment-159493 Share on other sites More sharing options...
zfred09 Posted January 12, 2007 Author Share Posted January 12, 2007 I get string errors in the new file in places such as this because the quote after the echo should be a ' but I had to change it or the write file would error out[code]echo("<form method="post" action="auth.php"><p>Username: <input type="text" name="userid" id="myform" maxlength="20" size="12"/></p> <p>Password : <input type="password" name="userp" id="myform" maxlength="20" size="12"/></p> <input type="submit" value="Login" class="gobutton"/></font> </form>");[/code] Link to comment https://forums.phpfreaks.com/topic/33958-fwrite-and-php/#findComment-159505 Share on other sites More sharing options...
Jessica Posted January 12, 2007 Share Posted January 12, 2007 Every time you have a " inside a string with a ", you have to escape it. You're still not doing that. If you want to always use " use this: \"echo("<form method=\"post\" Link to comment https://forums.phpfreaks.com/topic/33958-fwrite-and-php/#findComment-159506 Share on other sites More sharing options...
ShogunWarrior Posted January 12, 2007 Share Posted January 12, 2007 It won't error and you can keep everything the same as long as: * You remeber not to include the type of quote (" or ') that surrounds a string in that string without escaping it:[b]\"[/b] or [b]\'[/b] Link to comment https://forums.phpfreaks.com/topic/33958-fwrite-and-php/#findComment-159507 Share on other sites More sharing options...
zfred09 Posted January 12, 2007 Author Share Posted January 12, 2007 Ah yes that works, I didn't understand/know what escaping the quotes was so thanks for the explanation. Problem Solved. Link to comment https://forums.phpfreaks.com/topic/33958-fwrite-and-php/#findComment-159509 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.