File Creation from template

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gjb79
Forum Commoner
Posts: 96
Joined: Fri Jul 18, 2003 6:35 am
Location: x <-- (DC)
Contact:

File Creation from template

Post by gjb79 »

Hi,

:arrow: 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

could you explain the take a php template and save it part in more detail?
User avatar
gjb79
Forum Commoner
Posts: 96
Joined: Fri Jul 18, 2003 6:35 am
Location: x <-- (DC)
Contact:

Figures

Post by gjb79 »

:idea: 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!
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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.
User avatar
gjb79
Forum Commoner
Posts: 96
Joined: Fri Jul 18, 2003 6:35 am
Location: x <-- (DC)
Contact:

Almost

Post 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
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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

&lt;html&gt;
&lt;head&gt;
&lt;title&gt; Form to PHP Test &lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form method="post" action="test.php"&gt;
Name of new Template &lt;input type="text" name="pagename"&gt;&lt;br&gt;
&lt;input type="submit" value="create template"&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

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.
User avatar
gjb79
Forum Commoner
Posts: 96
Joined: Fri Jul 18, 2003 6:35 am
Location: x <-- (DC)
Contact:

YAY!!!!

Post by gjb79 »

:D That worked perfectly Gen-ik!!!

Thanks a Million!
Post Reply