View Full Version : Create file and fill it with PHP code

01-09-2010, 06:37 AM

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

$File = "../$page.php";
$Handle = fopen($File, 'w');
$Data = "what i want to print la la la";
fwrite($Handle, $Data);

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.

01-09-2010, 07:32 AM
try e.g.

<table width=120 border=1 bgcolor="#EACA19">
<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">
<input type="submit" value="Send">


//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]);

01-09-2010, 07:57 AM
or similar to it:

<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%">
<td width="60%"><input type="text" name="something" size="20" value="<?php echo $something ;?>" /></td></tr>
<td align="left"><input class="button" type="submit" value="Update" onclick="javascript: history.go(-1)" />

//as file.php
$File = "../$page.php";
$Data =
chr(47)."*\n coding created by sir.jones \n*".chr(47)."\n\n".
chr(36)."something = ".chr(39).$_POST['something'].chr(39).";\n".
$Data = stripcslashes($Data);
/* Writing file configurations */
$Handle = fopen($File, "w+");
fwrite($Handle, "$Data");

01-09-2010, 04:00 PM
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() (http://www.php.net/file_put_contents) rather than fopen fwrite fclose

01-09-2010, 05:07 PM
'' 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 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'>
<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" />

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


EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum