www.webdeveloper.com
Results 1 to 8 of 8

Thread: Create a PHP page with PHP

  1. #1
    Join Date
    Sep 2007
    Posts
    13

    Talking 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 05:11 PM.

  2. #2
    Join Date
    Sep 2011
    Location
    Bristol, England, United Kingdom
    Posts
    192
    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.
    Last edited by George88; 07-31-2012 at 06:06 PM.

  3. #3
    Join Date
    Sep 2007
    Posts
    13

    It worked!

    Thanks, George88! Works perfectly!

  4. #4
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,268
    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);// 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"=>"&#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;
    
    // Lines to define two array : $usr and $ensLng
    		/*
    		...
    		*/
    	$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.='?>';
    
    // 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

  5. #5
    Join Date
    Aug 2012
    Posts
    8
    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.

  6. #6
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,268
    I does not understand the interest to encode to decode, I believed that fwrite added the BOM?

  7. #7
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by 007Julien View Post
    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?

  8. #8
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    99% sure fwrite() won't add a BOM on its own.
    Jon Wire

    thepointless.com | rounded corner generator

    I agree with Apple. Flash is just terrible.

    Use CODE tags!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles