Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to dynamically write PHP files using the fwrite() function and it just does not seem to be working. The code is definitely in the string being written, but then when I open the file that was written it just isn't there! My assumption is that the PHP compiler is, for whatever reason, reading the PHP tags in the string, executing what's between them, and then abandoning them. Is this assumption accurate? What can I do to get around it?

The code looks something like this:

$code = "<?php echo "This is a dynamically written file!"; ?>";
$openFile = fopen("filename.php","w");
fwrite($openFile,$code);
fclose($openFile);

I have tried both single and double quotes around the 'code' variable.

EDIT: I tried single quotes, but then the single-quoted variable was mixing with a double-quoted variable and converting it. I feel dumb. Sorry for wasting everybody's time.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
443 views
Welcome To Ask or Share your Answers For Others

1 Answer

Try the following so PHP doesn't parse the content of your string (single quotes):

$code = '<?php echo "This is a dynamically written file!"; ?>';
$openFile = fopen("filename.php","w");
fwrite($openFile,$code);
fclose($openFile);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...