wintallo Posted May 31, 2007 Share Posted May 31, 2007 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 More sharing options...
per1os Posted May 31, 2007 Share Posted May 31, 2007 Post an example of the created file. You may try this for the string $movie_page = "<?php\n echo \"Hello, world!\";\n ?>"; Link to comment https://forums.phpfreaks.com/topic/53755-solved-using-php-to-write-a-php-file/#findComment-265676 Share on other sites More sharing options...
wintallo Posted May 31, 2007 Author Share Posted May 31, 2007 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!"; ?> Link to comment https://forums.phpfreaks.com/topic/53755-solved-using-php-to-write-a-php-file/#findComment-265681 Share on other sites More sharing options...
per1os Posted May 31, 2007 Share Posted May 31, 2007 Should work, are the permissions properly set? And a side note you need \n not just phpn =) $movie_page = "<?php\n echo "Hello, world!\";\n ?>"; Link to comment https://forums.phpfreaks.com/topic/53755-solved-using-php-to-write-a-php-file/#findComment-265687 Share on other sites More sharing options...
cmgmyr Posted May 31, 2007 Share Posted May 31, 2007 This worked for me: <?php $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); ?> Link to comment https://forums.phpfreaks.com/topic/53755-solved-using-php-to-write-a-php-file/#findComment-265691 Share on other sites More sharing options...
wintallo Posted June 1, 2007 Author Share Posted June 1, 2007 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 <? $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); ?> Link to comment https://forums.phpfreaks.com/topic/53755-solved-using-php-to-write-a-php-file/#findComment-265969 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.