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

Categories

0 votes
460 views
in Technique[技术] by (71.8m points)

fwrite - Writing PHP file not working

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

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

1 Answer

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...