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
You can open the PHP file in question as a string using file_get_contents(), the use fopen() to save the file out as a new file with an extension of your choice. For example:
PHP Code:
<?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); ?>
You'll need to create a .txt 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.
<?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);// carriage return
$tab=chr(9);// tabulation
$ans="2012-13";// a data
// Build the file
$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"=>"École élémentaire Marcel Lafitan",'.$all.$tab.$tab;
$chn.='"cp"=>"Cours préparatoire",'.$all.$tab.$tab;
$chn.='"ce1"=>"Cours élémentaire 1ère année",'.$all.$tab.$tab;
$chn.='"ce2"=>"Cours élémentaire 2ème année",'.$all.$tab.$tab;
$chn.='"cm1"=>"Cours moyen 1ère année",'.$all.$tab.$tab;
$chn.='"cm2"=>"Cours moyen 2ème année");'.$all.$all.$tab;
// Lines to define two array : $usr and $ensLng
/*
...
*/
$chn.='// Les utilisateurs (ordre alphabétique, à l\'exception de la directrice en premier)'.$all.$tab;
$chn.='$f_rngUsr = array("'.implode('","',$usr).'");'.$all.$tab;
$chn.='// Les enseignants noms complets (même classement par rangs)'.$all.$tab;
$chn.='$f_rngEns = array("'.implode('","',$ensLng).'");'.$all.$all.$tab;
$chn.='?>';
// Write the file
$fch="inc_config.$ans.txt";
$hdl=fopen($fch,'wb');
fwrite($hdl,$chn);fclose($hdl);
rename($fch,"inc_config.$ans.php");
?>
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 ?
Last edited by 007Julien; 08-02-2012 at 07:10 AM.
Reason: compléments
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
I does not understand the interest to encode to decode, I believed that fwrite added the BOM?
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?
Bookmarks