Thank you ahead of time for any help you might have.
File Creation from template
Moderator: General Moderators
File Creation from template
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.
Thank you ahead of time for any help you might have.
Figures
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!
Copying a file is pretty simple.....
..... 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.
Code: Select all
<?php
$copyFrom = "somefile.php";
$copyTo = "thecopy.php";
copy($copyFrom, $copyTo);
?>Almost
I did a quick test with exactly what Gen-ik said, It worked!
Now I need to have it dynamically rename the file.
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
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);
?>This script however doesn't seem to work.
Where did I go wrong?
Thanks again
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. )
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. )
That should do the trick.
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!!!!
Thanks a Million!