Jump to content

[SOLVED] Write a PHP file with PHP


Doqrs

Recommended Posts

Hello

 

I am looking to use fwrite to write a php script just as a counter opens up a .txt file.

 

$filename = "dir/" . $time . ".php";
$filewrite = -My PHP script here with <? stuff ?>-
$fp = fopen($filename, "w+");
fwrite($fp, $filewrite);
fclose($fp);

Link to comment
Share on other sites

thank you

 

how would i be able to get the <<<EOF heredoc stuff to work if i also want to have actual php code and not just text? I wasn't able to get it to work with this:

 

<?php
$contents = <<<EOF
header("location: somewhere");
EOF;

$fp = fopen("dir/{$time}.php", "w+");
fwrite($fp, $contents);
fclose($fp);
?>

Link to comment
Share on other sites

Yep, that code snippet should work fine, as Doqrs says. Maybe you have some indentation in your real code that makes it fail, like :

if(smthing) {
  $contents = <<<EOF
  header("location: somewhere");
  EOF;
}

 

In this case, you will get an error, because you cant have spaces or tabs or anything other than "EOF;" on the EOF line.

 

If it's your fopen/fwrite that fails you should check file/folder permissions.

http://no2.php.net/manual/en/function.fopen.php

http://no2.php.net/manual/en/function.fwrite.php

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.