treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Get PHP to create pages.

  • 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.
    Basically:
    template.html -> PHP -> template-copy.html
  • so you want to make a copy of the html file with PHP?
  • $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
  • 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()
  • 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.
  • copy() clones the file and it's contents and creates an entirely new file with those contents.
  • 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?
  • Maybe you should give a detailed overview of what you are actually trying to do? At the moment it seems like everyone is guessing :)
  • 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.
  • 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:

    if(file_exists($filename)){
    $handle = fopen($filename, 'r');
    $size = filesize($filename);
    $file = fread($handle, $size);
    fclose($handle);

    //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.
  • 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.
  • $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.