Web Analytics Made Easy -
StatCounter What is the best way to include() php file with $vars into new file? - CodingForum

Announcement

Collapse
No announcement yet.

What is the best way to include() php file with $vars into new file?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • What is the best way to include() php file with $vars into new file?

    What I want to accomplish is:
    • Read a php file (template.php)
    • pass variables to it ($strings)
    • write a new file with all variables evaluated


    I can include(template.php); to the browser just fine, with all php code evaluated. I just don't know how to output to a file.

    Here is the code so far:
    test.php
    PHP Code:
    $name='Red';
    $last='Matrix';
    $real='David';

    $template='path/to/template.php';

    include 
    $template
    template.php
    PHP Code:
    <p>Your name is: <?php echo $name?> <?php echo $last?></p>
    <p>But your real name is <?php echo $real?></p>
    HTML output to browser works fine:
    Code:
    <p>Your name is: Red Matrix</p>
    <p>But your real name is David</p>
    How do I write the HTML to a new file called whatever.html?
    I've tried the following to no avail:

    PHP Code:
    //create file
    $file="whatever.html";
    $newfile fopen($file'w') or die("Can't Create File");
    fwrite($newfile,$template);
    fclose($newfile);

    //another approach
    eval("\$template = \"$template\";");
    $file="whatever.html";
    $newfile fopen($file'w') or die("Can't Create File");
    fwrite($newfile,$template);
    fclose($newfile);

    //variants in above codes
    $template < include $template // writes nothing to $file
    $template = include $template // writes "1" to $file 
    Thanks for your time.

  • #2
    http://us2.php.net/manual/en/functio...t-contents.php
    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

    Comment


    • #3
      The below code you posted is correct providing you just want to write the string $template to a file
      PHP Code:
      //create file
      $file="whatever.html";
      $newfile fopen($file'w') or die("Can't Create File");
      fwrite($newfile,$template);
      fclose($newfile); 
      This one would work. The only reason it might not be working is possibly because the file you are writing to hasn't got write permissions. If its a linux server chmod 777 it. If its windows set it write permissions to All.

      The second code uses eval for what reason i am unclear and this could be dangourous, looks like pointless code. For instance what creates template? If its a string that is put together with user posted data then definitely don't do that or your opening yourself for a massive security risk. processing user submitted data with eval is an awful idea. Are you trying to get it to save something like
      Code:
      $template = 'some text';
      to your template file so it can work with include. I wouldn't do that.
      I would just save it as normal text data. Then when i read the file it would automatically be passed to a string variable using fread or file_get_contents. But if you really think you do have to do it this way then the eval statment and the string is wrong you would have to find out the ascii code for $ and save that to string so it deosn't translate into a variable. So you would create it with chr

      PHP Code:
      chr(36); 
      Then turn it into a string

      PHP Code:
      $tamplate chr(36).'template="'.$template.'";'
      Then use the fwrite to write $template to the file then you can use include but again i would just use fread or file_get_contents. And i have no idea about your third code?
      You can not say you know how to do something, until you can teach it to someone else.

      Comment


      • #4
        Thanks. This did it for me.

        Here's the real code:
        PHP Code:
        $html 'themes/'.$theme.'/template.php'//define template path and filename
                
        ob_start();
            include(
        $html); //get template file, parse variables
            
        $template ob_get_contents(); // store HTML in $template
        ob_end_clean();
                
        mkdir("$category"0777); //if doesn't exist, create it
        $file "$category"."/"."$name".".html"// new poll file name  $category/$name.html
        $pollname fopen($file'w') or die("can't open file");
        fwrite($pollname$template); // write the HTML with all our variables evaluated
        fclose($pollname);
        echo 
        '<a href="'.$file.'">Click Here</a> to view the poll<br />'

        Comment

        Working...
        X