Treehouse: Grow your CSS skills. Land your dream job.

Get PHP to create pages.

  • # March 6, 2012 at 2:09 pm

    So I have this html template, and I would like PHP to look at this template, and make a new html page, that is a copy of this, and only change the name of the file.
    template.html -> PHP -> template-copy.html

    # March 6, 2012 at 3:03 pm

    so you want to make a copy of the html file with PHP?

    # March 6, 2012 at 7:07 pm
    $file = 'template.html';
    $newfile = 'template-copy.html';

    if (!copy($file, $newfile)) {
    echo "failed to copy $file...n";

    *Taken from PHP manual and modified file names

    # March 7, 2012 at 3:34 am

    You’ll need to make sure you have permissions to create files in the directory. So if you’re copying fails due to permissions, take a look at chmod()

    # March 7, 2012 at 4:38 pm

    I don’t understand karlpcrowley’s code use. That’s just talking about filenames or so. I want to create a new file entirely, but that has the same content.

    And I can give it read and write permissions. That’s OK.

    # March 8, 2012 at 3:38 am

    copy() clones the file and it’s contents and creates an entirely new file with those contents.

    # March 8, 2012 at 11:48 am

    I’m testing the import/outport, where I may be able to edit variables within the imported file before exporting it.
    Can I do this?

    # March 8, 2012 at 11:54 am

    Maybe you should give a detailed overview of what you are actually trying to do? At the moment it seems like everyone is guessing :)

    # March 9, 2012 at 12:51 pm

    I create an HTML template with PHP variables for in certain places.

    I then run a PHP script, that copies the template, giving the variables a different value each time (might this work with input/output?)

    A new page is created, that has the same structure as the template, with only the variables changing.

    # March 15, 2012 at 5:13 pm

    forgive me for asking, but…

    The PHP page itself can be an html template with variables that changed when parsed and sent to the browser, so why have PHP read a static file and change parts to write another file? Are you intending on keeping the copy and always making new copies?

    What you’ll need to do is something like:

    $handle = fopen($filename, 'r');
    $size = filesize($filename);
    $file = fread($handle, $size);

    //put variable scanning and changing code here

    echo file_put_contents($newfilename, $newdata) ? 'Success!' : 'Epic Fail!';

    How you scan and change the file will depend on what you are doing. You could use the strtr() function to translate variable code in the file to something else.

    # March 17, 2012 at 3:43 am

    Thanks for responding.

    Could you explain what that does please? (and what is variable scanning?)
    I’m rather new to PHP, so I don’t get what passing the variables to the functions makes them do.

    # March 28, 2012 at 4:51 am

    $file = ‘template.html';
    $newfile = ‘template-copy.html';

    if (!copy($file, $newfile)) {
    echo “failed to copy $file…n”;

    With that, could I have a dialogue box that the customer could put in the name that they wanted? Say I have template.html and I add content to make it a blog. Then a window (or something) comes up and asks what I want to save it as. Then the modified template would become blog.html. Also, would there be any trouble viewing the new pages? Sorry if that made little sense. It’s really late and I’m on my iPad.

Viewing 12 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic.