Forum Moderators: coopster
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
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.
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.
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.
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
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.
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 12Warning: 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