Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jan 2010
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Create file and fill it with PHP code

    Hello,

    I am writing somewhat like a CMS for my site, and I am struggling with the following moment :

    I create a new page from admin section, it automatically creates menu item and a .php file. The problem is, how can I fill the file with php code... When I write
    PHP Code:
    $File "../$page.php";
    $Handle fopen($File'w');
    $Data "what i want to print la la la";
    fwrite($Handle$Data);
    fclose($Handle); 
    Everything is nice and easy, but how do I transfer the whole filling of standart index.php through this $Data variable? I get parse error because PHP tries to interpret the code inside this variable no matter how hard I tried. Maybe there are also other ways to do this?

    Basically I need to transfer bits of code with some assigned variables from this page without interpreting it to a file.
    Last edited by RxDx; 01-09-2010 at 05:58 AM.

  • #2
    New Coder
    Join Date
    Dec 2009
    Posts
    84
    Thanks
    6
    Thanked 3 Times in 3 Posts
    try e.g.

    Code:
    <table width=120 border=1 bgcolor="#EACA19">
    <tr><td>
    <form method="POST" action="file.php">
    name:<input size="15" maxlength="15" type="text" name="name">
    Address:<input size="15" maxlength="25" type="text" name="Addr">
    <center>
    <input type="submit" value="Send">
    </center>
    </form>
    </td></tr></table>
    then:
    PHP Code:
    //as file.php
    $File "../$page.php";
    if (!
    $name)
    $name ">>"; }
    else 
    $name .= ":";

    $Addr preg_replace("/</","&lt;",$Addr);
    $Addr preg_replace("/>/","&gt;",$Addr); 

    $comfile file($File);
    if (
    $Addr != "") {
    if (
    strlen($Addr) < $MAX_LENGTH) {
    $Handle fopen ($File"w");
    $Addr stripslashes($Addr);
    fwrite ($Handle"<div style='width:112px; overflow:hidden'><i>$name</i> $Addr</div>\n");
    for (
    $i 0$i $NUM_COMMENTS$i++) {
    fwrite ($Handle$comfile[$i]);
    }
    }
    fclose($Handle);

    Last edited by sir.jones; 01-09-2010 at 06:38 AM.

  • #3
    New Coder
    Join Date
    Dec 2009
    Posts
    84
    Thanks
    6
    Thanked 3 Times in 3 Posts
    or similar to it:
    Code:
    <table border="0" cellpadding="10" cellspacing="0" width="100%">
    <form action="file.php" method="post" name="return_url" />
     <table border="0" cellpadding="5" cellspacing="5" width="100%">
    <tr>
     <td width="60%"><input type="text" name="something" size="20" value="<?php echo $something ;?>" /></td></tr>					
    </table>
    </td></tr>	  
    <tr>
    <td align="left"><input class="button" type="submit" value="Update"  onclick="javascript: history.go(-1)" />
    </td></tr>
    </form>

    PHP Code:
    <?php
    //as file.php
    $File "../$page.php";
    extract($_GET);
    extract($_POST);
    $Data 
    chr(60)."?php\n".
    chr(47)."*\n coding created by sir.jones \n*".chr(47)."\n\n"
    chr(36)."something = ".chr(39).$_POST['something'].chr(39).";\n".
    "\n?".chr(62);
    $Data stripcslashes($Data);
    /* Writing file configurations */
        
    $Handle fopen($File"w+");
        
    fwrite($Handle"$Data");
        
    fclose($Handle);
    ?>
    Last edited by sir.jones; 01-09-2010 at 07:11 AM.

  • #4
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    If you mean your variables like $a, $b and so on are getting converted it's because they're inside double quotes and it does that by default. You can get around that by using single quotes

    Also I would recommend looking at file_put_contents() rather than fopen fwrite fclose
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #5
    New Coder
    Join Date
    Jan 2010
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts
    '' don't seem to work in some cases, as I have connection to the database in the file.
    I'll post a code here. I need this text to be transfered to a file. The problem is that it wants to interpret <?php things on that page, and doesn't write to file. I have modified a few things, but the place with meta_k, meta_d and same ones over the file don't work. How do I modify it to be a normal variable to transfer this text to file?

    PHP Code:
    <?php include ('mysql.php'); 



    $result mysql_query ("SELECT title, meta_d, meta_k, text FROM settings WHERE page='index'"$db);
    $info mysql_fetch_array($result);




    ?>



    <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
    <html xmlns='http://www.w3.org/1999/xhtml'>
    <head>
    <meta http-equiv='Content-Type" content="text/html; charset=utf-8' />
    <meta name='description' content="<?php echo $info['meta_d']; ?> " />
    <meta name='keywords' content= "<?php echo $info['meta_k']; ?>  " />

    <title><?php echo $info['title']; ?></title>
    <link href="style.css" rel="stylesheet" type="text/css" />
    </head>

    <body>
    <table width="690" border="0" align="center" bgcolor="#FFFFFF" class="main_border">
     <?php include ("blocks/header.php");?>
      <tr>
      <?php include ("blocks/left.php"); ?>
              <td valign="top"><?php echo $info['text']; ?></td>
            </tr>
          </table></td>
      </tr>
     <?php include ("blocks/footer.php");?>
    </table>



    </body>
    </html>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •