Hey guys, whats the best way of creating PHP files using php?
I want to create a new file for each article for example, how to do that?
I want the file to be created using PHP because the content in the file might change later on .
The file is something like that…
static header
Changeable content (taken from a database)
static footer
I had it working that way but i’m using many APIs such as facebook send button which doesnt accept page parameters, eg if u got a page called getarticles.php?ID=5 it will ignore everything after the ‘?’, making it useless…
I know its horrible (facebook’s fault) but i dont have any other solution to do that…
get is not a solution because they dont accept page parameters
post is not a solution either because then all articles will be under 1 page that will have the same name always, like articles.php … (correct me if im wrong?)
You can still send post parameters to that assuming at any point you’re submitting a form, you could always make your form action submit to itself and the form submission be an <a> link.
that will still not make my API work as the send button (for example) accepts a link only, so it will still get articles.php and have no clue to which article to point.
You’re probably running Apache. If you aren’t, you’re likely running nginx. If you are running anything else, I recommend changing hosts. =p
Both of those are web servers, which are what make it possible for your pages to be a website. It runs under PHP.
With Apache, you just create a file named .htaccess and put it in the root of your site. With nginx, you have to put it somewhere a little different, but it’s still pretty similar.
To answer your original question (even though getting the urls to work is probably better), do the same thing you would to generate an html page. Have a template with something like:
// some_demo.php.tremplate
// Here is a php function that will end up in my generated php file
function some_demo()
{
$a = "<?php echo $aPassed; ?>";
echo $a;
}
And then use a basic template processing approach:
// Here I am with my generator
// Want to pass this to the generated file
$aPassed = 'some demo info';
// Pull in the template
ob_start();
include 'demo.php.template';
$body = ob_end_clean();
// And store it with a php tag
file_put_contents('generated.php',"<?\
" . $body);
This is also handy for generating caches file of one sort or another.