/    Sign up×
Community /Pin to ProfileBookmark

Create a PHP page with PHP

I know the title sounds weird, but what I want is to create and store a page that uses php from a php script in another page. It must be possible, since I should be able to generate the whole page as a string and scape the ” and $ characters, but I don’t know how. Does anyone can give me the answer or point to a tutorial or search terms I could use? Thanks

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@George88Jul 31.2012 — You can open the PHP file in question as a string using [FONT="Courier New"]file_get_contents()[/FONT], the use [FONT="Courier New"]fopen()[/FONT] to save the file out as a new file with an extension of your choice. For example:
[code=php]<?php
// Create a new file with fopen.
$handle = fopen("new_text_file.txt", "w");

// Get the contents of the original file.
$original_file = file_get_contents("original_file.txt", true);

// Write the new file based on the above.
$write = fwrite($handle, $original_file);
?>
[/code]

You'll need to create a [i].txt[/i] file in the same directory called "original_file" with some data inside it, such as "Hello World". Once the code runs, it'll copy the contents of that file and write it out as a new file.
Copy linkTweet thisAlerts:
@HaniverauthorAug 01.2012 — Thanks, George88! Works perfectly!
Copy linkTweet thisAlerts:
@007JulienAug 02.2012 — It's always possible to write something like this
[CODE]<?php
header("Content-Type: text/html; charset=utf-8");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
$all=chr(10);[COLOR="Blue"]// carriage return[/COLOR]
$tab=chr(9);[COLOR="Blue"]// tabulation[/COLOR]
$ans="2012-13";[COLOR="Blue"]// a data[/COLOR]

[COLOR="Blue"]// Build the file[/COLOR]
$chn='<?php'.$all.$all.$tab.'/* Fichier de configuration */'.$all.$all.$tab;

$chn.='// Les titres en fonction des pages ou niveaux'.$all.$tab;
$chn.='$f_pagTtl = array('.$all.$tab.$tab;
$chn.='"accueil"=>"&#201;cole &#233;l&#233;mentaire Marcel Lafitan",'.$all.$tab.$tab;
$chn.='"cp"=>"Cours pr&#233;paratoire",'.$all.$tab.$tab;
$chn.='"ce1"=>"Cours &#233;l&#233;mentaire 1&#232;re ann&#233;e",'.$all.$tab.$tab;
$chn.='"ce2"=>"Cours &#233;l&#233;mentaire 2&#232;me ann&#233;e",'.$all.$tab.$tab;
$chn.='"cm1"=>"Cours moyen 1&#232;re ann&#233;e",'.$all.$tab.$tab;
$chn.='"cm2"=>"Cours moyen 2&#232;me ann&#233;e");'.$all.$all.$tab;

[COLOR="Blue"]// Lines to define two array : $usr and $ensLng
/*
...
*/[/COLOR]
$chn.='// Les utilisateurs (ordre alphab&#233;tique, &#224; l'exception de la directrice en premier)'.$all.$tab;
$chn.='$f_rngUsr = array("'.implode('","',$usr).'");'.$all.$tab;
$chn.='// Les enseignants noms complets (m&#234;me classement par rangs)'.$all.$tab;
$chn.='$f_rngEns = array("'.implode('","',$ensLng).'");'.$all.$all.$tab;
$chn.='?>';

[COLOR="Blue"]// Write the file[/COLOR]
$fch="inc_config.$ans.txt";
$hdl=fopen($fch,'wb');
fwrite($hdl,$chn);fclose($hdl);
rename($fch,"inc_config.$ans.php");
?>[/CODE]


The problem is with utf-8... A configuration file build with this method contains a BOM which causes troubles in include files (except for the last PHP versions).

Then the question is how to write dynamically a PHP file without BOM ? I am not sure the method supra is the best to avoid this BOM ?
Copy linkTweet thisAlerts:
@FSDesignsAug 02.2012 — You could always Base64 encode it and then decode it as you write it with fwrite().

(I know many of you don't like the idea of it, but it gets around many issues)

Hit me up with a PM if you'd like some help ?


-FS Designs.
Copy linkTweet thisAlerts:
@007JulienAug 02.2012 — I does not understand the interest to encode to decode, I believed that fwrite added the BOM?
Copy linkTweet thisAlerts:
@criterion9Aug 03.2012 — I does not understand the interest to encode to decode, I believed that fwrite added the BOM?[/QUOTE]

I have never had fwrite produce a BOM. It has always been a "fancy" editor trying to do something for me I didn't ask for. Are you sure it is producing a BOM?
Copy linkTweet thisAlerts:
@svidgenAug 03.2012 — 99% sure fwrite() won't add a BOM on its own.
×

Success!

Help @Haniver spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...