Page 1 of 1
File Creation from template
Posted: Sat Oct 11, 2003 10:46 am
by gjb79
Hi,

I know this can be done, but I have no clue where I am supposed to look to learn how to do it. I've scanned over many manuals and guide books with little luck.

I would like to know (or atleast be pointed in the right direction of what/where to look for) how to use php (with mysql) to take a php template and save it in a different directory with file name created from a form element.
Thank you ahead of time for any help you might have.
Posted: Sat Oct 11, 2003 12:33 pm
by volka
could you explain the take a php template and save it part in more detail?
Figures
Posted: Sat Oct 11, 2003 12:34 pm
by gjb79

I found something in one of the manual I have. I guess I skipped over it before. Let me know if there is a better way of doing this
To create a file it says to use the touch () function:
int touch (string file, [int time])
It also says, which is more of what I had in mind, to use copy () and rename ().
copy ($source_path, $destination_path);
rename ($oldfile, $newfile);
So I assume I could create a template file in directory A. The script would then copy the template from directory A to directory B, and then rename it to whatever I need it to be. I would then be able to reuse the template in directory A to create many files in directory b each based off of the original template.
If anybody has a method that is more efficient than this, or a different method all together, I'd love to hear it. Knowledge is power.
Thanks!
Posted: Sat Oct 11, 2003 12:40 pm
by Gen-ik
Copying a file is pretty simple.....
Code: Select all
<?php
$copyFrom = "somefile.php";
$copyTo = "thecopy.php";
copy($copyFrom, $copyTo);
?>
..... that will copy and rename the file in one go. If the $copyTo file already exists then it will be overwritten with the new copy.
Almost
Posted: Sat Oct 11, 2003 2:56 pm
by gjb79
I did a quick test with exactly what Gen-ik said, It worked!
Now I need to have it dynamically rename the file.
Code: Select all
<?php
$copyFrom = "templates/pagetemplate.php";
$copyTo = "../site/" . $pagename . ".php";
copy($copyFrom, $copyTo);
?>
A form is filled out and submitted. the field is called pagename. I do have the register globals enabled.
This script however doesn't seem to work.
Where did I go wrong?
Thanks again
Posted: Sat Oct 11, 2003 4:49 pm
by Gen-ik
It could be a number of things really, and yes it does make a difference if global_variables are turned on or off.
If you try the following stuff out it should work.
THE FORM PAGE ( called anything you like. )
Code: Select all
<html>
<head>
<title> Form to PHP Test </title>
</head>
<body>
<form method="post" action="test.php">
Name of new Template <input type="text" name="pagename"><br>
<input type="submit" value="create template">
</form>
</body>
</html>
THE PHP PAGE ( call it test.php for now and place it in the same place as the form page. If you call it anything else remember to change the ACTION value in the FORM. )
Code: Select all
<html>
<head>
<title> Form to PHP Test </title>
</head>
<body>
<?php
$pagename = $_POST["pagename"]; // gets the form variable if globals are on or off
$copyFrom = "templates/pagetemplate.php";
$copyTo = "../site/" . $pagename . ".php";
$hasItWorked = copy($copyFrom, $copyTo);
if($hasItWorked) echo "IT HAS WORKED!";
if(!$hasItWorked) echo "DOH! IT HASN'T WORKED!";
?>
</body>
</html>
That should do the trick.
YAY!!!!
Posted: Sun Oct 12, 2003 9:07 am
by gjb79

That worked perfectly Gen-ik!!!
Thanks a Million!