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
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!";
?>

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.