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
Last edited by Haniver; 07-31-2012 at 06:11 PM.
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:
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.
// 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);
Last edited by George88; 07-31-2012 at 07:06 PM.
Thanks, George88! Works perfectly!
It's always possible to write something like this
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).
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");
$all=chr(10);// carriage return
$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.='"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;
// Write the file
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 08:10 AM.
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?
Originally Posted by 007Julien
99% sure fwrite() won't add a BOM on its own.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)