Jump to content

[SOLVED] Using PHP to write a PHP file


wintallo

Recommended Posts

Hello,

 

I'm writing a PHP application that has a component that writes other PHP files. I have the fopen, fwrite, and fclose all working correctly. The problem is, is that when it writes the new PHP file, and I open it, my server throws a 500 error. When I open the new PHP file in a text editor, It looks like perfectly valid PHP. Here's my code.

 

<?
$movie_page = "<? echo \"Hello, world!\"; ?>";
$movie_page_name = "test.php";
$handler = fopen($movie_page_name, 'w');
fwrite($handler, $movie_page);
fclose($handler);
?>

 

Is there some special parameter I have to use to write PHP files using PHP?

Link to comment
https://forums.phpfreaks.com/topic/53755-solved-using-php-to-write-a-php-file/
Share on other sites

Okay, I revised my code as you said. Here is the code for the PHP file that creates the other PHP files.

 

<?
$movie_page = "<?php\n echo \"Hello, world!\";\n ?>";
$movie_page_name = "test.php";
$handler = fopen($movie_page_name, 'w');
fwrite($handler, $movie_page);
fclose($handler);
?>

 

And the file that code creates is

 

<?php
echo "Hello, world!";
?>

Okay, I found my problem. When I create a new file using PHP, it gives it a permissions value that does not let the outside world see its contents. So I chmod'd it to 0644, which apparently is sufficient. For future reference, this is my final working code  ;D

 

<?
$movie_page = "<?php\n echo \"Hello, world!\";\n ?>";
$movie_page_name = "test.php";
$handler = fopen($movie_page_name, 'w');
fwrite($handler, $movie_page);
fclose($handler);
chmod($movie_page_name, 0644);
?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.