Forum Moderators: coopster

Message Too Old, No Replies

Opening template page and adding variable in PHP

just one line of code into this template.

         

wruk999

11:42 am on Feb 23, 2003 (gmt 0)

10+ Year Member



Hi,

does anyone know whether it is possible, and if so, which commands to use, when I need to do the following:

- Use a web-based PHP form, to input a $cat variable, and $pagename variable
- Take these two variables, open a template file, which has html/php already in, and write the following line at the top:

<? $cat = $catvariablefromabove;?>

then close the file, BUT save it as.. $pagename.php, leaving the template file without the variable at the top, so it can be used again & again

any ideas or help will be very much appreciated :)

Regards,
William

hakre

12:51 pm on Feb 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hi wruk999,

that's quite a simple thing, here is some code:

$oldfilecontent = contains the complete content of the first file.

$newfile = @fopen($pagename.'.php', 'w');
@fputs($newfile, '<? $cat = '.$cat.';?>'."\n" );
@fputs($newfile, $oldfilecontent);
@fclose($newfile);

hope that helps. if you don't now how to read the oldfile / template in, just post your question.

wruk999

12:58 pm on Feb 23, 2003 (gmt 0)

10+ Year Member



hi hakre,

thanks for the info - just one thing...how does the web server know where to put the newfile.

how would I get it to put it in /www/hosts/mydomin.com?

the bit about opening/reading the template..would i use

fopen(/www/hosts/mydomain.com/template.php,'r')

?

KR,
william

wruk999

1:31 pm on Feb 23, 2003 (gmt 0)

10+ Year Member



hi,

i have this code on my page - no joy..

<form method="post" action="<? echo $PHP_SELF?>">
<p>Pagename:
<input type="text" name="pagename">
</p>
<p>CatID:
<input type="text" name="catid">
</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>
<?
if($submit)
{

$oldfilecontent = fopen("/www/hosts/my-domain/templates/template.php", "r+");

$newfile = @fopen($pagename, 'w');
@fputs($newfile, '<? $cat = '.$catid.';?>'."\n" );
@fputs($newfile, $oldfilecontent);
@fclose($newfile);

}

?>

I am not getting any errors back - just no files showing up. I am rnning the script from insde the directory I want the newfile to be written to. I have taken the .php bit out of the fputs file, as this is going to be in the formfield instead.

any ideas?

Any help will be very very much appreciated.

Regards.

Birdman

2:32 pm on Feb 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello,

The PHP Filesystem functions [php.net] index is a great resource. You can get the info you need right there.

Sorry I cannot give you any real help. I haven't got to file functions, myself.

wruk999

3:25 pm on Feb 23, 2003 (gmt 0)

10+ Year Member



hi,

I had a look through the resources which Birdman provided, and used an example they had on their page - just changed the file parameters, but got the cannot write to file error.

I even chown'd it to root.root, and chmod'd it 777 - still no joy.

[php.net...] is where I got the script from.

Any ideas?

William

hakre

3:33 pm on Feb 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



william, are you shure you're allowed to use the file functions on the server? if so, you can write to files. chmod 777 shouldn't be necessary.

if you use just the filename (no given path) the file should be created in the directory where the .php file is located.

remove the @ symbols in front of the commands to see if php returns error messages. the @ symbol prevents this if placed in front of a php command.

wruk999

3:50 pm on Feb 23, 2003 (gmt 0)

10+ Year Member



hi hakre,

here's the error output:

Warning: fputs(): supplied argument is not a valid File-Handle resource in /www/hosts/mysite/villages-dy/test.php on line 12

Warning: fputs(): supplied argument is not a valid File-Handle resource in /www/hosts/mysite/villages-dy/test.php on line 13

Warning: fclose(): supplied argument is not a valid File-Handle resource in /www/hosts/mysite/villages-dy/test.php on line 14

heres lines 12-14:

fputs($newfile, '<? $cat = '.$catid.';?>'."\n" );
fputs($newfile, $oldfilecontent);
fclose($newfile);

I'm just wondering whether having <? $cat = '.$catid.';?> (php tags) in the middle is throwing back the error?
or is there something obvious I have missed?

KR, william