Hello, I'm having a bit of trouble figuring out how to write php to a file. No matter how I escape it I get a slash before the closing > in the php tag.. fwrite($profileFilePointer, "<?php require('./system/files/profile.php'); ?>" ); PHP: I've tried fwrite($profileFilePointer, "<?php require('./system/files/profile.php'); ?/>" ); PHP: and a few other ways of escaping, but nothing worked yet. lol. thanks.
That's weird - if I do this: <?php $file = fopen ('somefile.php','w'); fwrite($file, "<?php require('./system/files/profile.php'); ?>" ); ?> PHP: it outputs this (in somefile.php): <?php require('./system/files/profile.php'); ?> PHP:
Just a guess, PHP tried to intrepret more inside double-quotes? Ref: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double Will the following work? fwrite($profileFilePointer, '<?php require('.'\'./system/files/profile.php\''.'); ?>' ); Code (markup):
There's no need to escape anything, really. All of the below works: <?php $file = fopen ('somefile.php','w'); fwrite($file, "<?php require('./system/files/profile.php'); ?>" ); fwrite($file, '<?php require("./system/files/profile.php"); ?>' ); fwrite($file, '<?php require(\'./system/files/profile.php\'); ?>' ); fwrite($file, '<?php require('.'\'./system/files/profile.php\''.'); ?>' ); ?> PHP: Output: <?php require('./system/files/profile.php'); ?> <?php require("./system/files/profile.php"); ?> <?php require('./system/files/profile.php'); ?> <?php require('./system/files/profile.php'); ?> PHP:
I think the problem is in some of the other code. I'll show the basic set up and what's happening. Not sure why, or how to fix it at the moment. I'm using expression web 4. <?php include('../system/scripts/phpclasses/systemclass.php'); require('../system/prime_system_data/sqlpassword.php'); //Some variables initiated here //some if statements and other code here...calls to database // Create User's Profile Page $profileFilePointer = fopen('../' . $_SESSION['userName'] . '.php','x'); //Include The Base File fwrite($profileFilePointer, '<?php require(\'./system/files/profile.php\'); ?>'); // The closing php tag here is blue as if not it quotes. //Close the files. fclose($profileFilePointer); } } ?> // this closing bracket has a yellow pointy bracket with error: extra bracket PHP:
You have two closing brackets, but since you're not showing the full file, no way to tell what's wrong. Try removing the bracket?
Try this: <?php fwrite($profileFilePointer, '<' . '?php require(\'./system/files/profile.php\'); ?' . '>'); ?> PHP: And are you sure "x" is the best mode for your situation in your fopen() call?
That looks like a good fix I thought that first bracket in the write function was closing off the php block, lol. Thanks I think x is, because the file will only be created once. I'm using the file being included as a base file for all profiles... There's probably no need for it to be x over some other method as it's only happening once...but I figured that would ensure that it wouldn't overwrite the profile on a second call...lol...seems like redundant security since I'm only writing to the file once, but what ever? lol.