Hi,

I have a file called reports.php which has several php and database functions. Upon viewing it in a browser it displays a nicely formatted report. Is there a way I can save the HTML(only) output from this reports.php into a new file (probably html) in a web server ? What i should be able to do is refer back to that new file anytime and be able to load it up on the browser just like it displayed the last time.

Edit: Also, is there a way to check if the process was completed and the new file was created by displaying amessage of some sort? I will need to clear my database after the file creation is done. that's why its important for me to check if the new file was successfully created.

Thanks.

Hi,

Here is simplest way of doing this (it is too simple that my comments out numbered my actual codes :)). Of course, you must search more on how to improve this. I am in the assumption here that file you are trying to cached is in your server. If it is coming from the external server, then process is slightly different than processes my sample codes will do.

Step One: in your server, create a directory called cache, and give this directory a permission CHMOD of 777 or 755 depending on your host's recommendation.

Step Two: copy codes below and name it anyNameYouWant.php.

<?php
    $cachetime = 10080 * 60;
    ## page naming can be anything it is all up to you
    $pageName = "somepage.html"; 
    $cachefile = "cache/".$pageName;

if(file_exists($cachefile) && (time() - $cachetime < filemtime($cachefile)))  {


    ## the page has been cached from an earlier request

    ## output the contents of the cache file

    include($cachefile); 

    }
else{

    ## we need to include a fresh file or the actual file where we can generate the cache file
    ob_start(); 
    ## include the page to be cached

    include 'pageToBeCached.php';

    ## prepare for writing the cache file
    $filePointer = fopen($cachefile, 'w');
    ## grab the output buffer and write it in cachefile
    fwrite($filePointer, ob_get_contents());
    ## close the file
    fclose($filePointer);
    ## Send the output to the browser
ob_end_flush();
echo "cache file created";

}
?>

Instruction for modification.

1.$cachetime = seconds * 60 ... gives you the time expiration before a new cache file is generated again.
2. $pagename = "somepageName.html"; This is all up to your... depending on your requirements this can be name something else it is all up to you.

3 include 'pageToBeCached.php'; must be change to include 'reports.php'; (based on your statements above).

  1. You must direct your browser to this page at all times, and not on the reports.php. This script will self-serve and do the job for you..

  2. In the bottom of this script remove echo "cache file created"; this line is just for testing.. it just need to be there on the first run of th escript..

Good luck, I hope this will help you... don't forget to mark this thread as solved if this helped you. Otherwise, post any error here..

commented: great answer! +0

Alternatively, we can use this class to create and include cache files. Although it i s a longer version of the above, this class can be extensible and reusable as many times as you want.

save this as cache.class.php

<?php
## Written and provided to you by Veedeoo or PoorBoy 2012
## this class has no warranty of any kind.
## feel free to use this to your application as needed
## feel free to extend this class for improved and better functionalities.
## What I meant by improved and better functionalities? Function like compression of cache file e.g. gzip.


    class MakeCache{

        private $content= null;
        private $cacheDuration = null;
        private $cacheLoc = null;
        private $cacheFile = null;


        public function __construct($content,$cacheDuration,$cacheLoc,$cacheFile){
         $this->loc = $cacheLoc;
         $this->file = $cacheFile;
         $this->duration = $cacheDuration;
         //$this->comp = $cacheCompression;
         $this->content = $content;

        }

        private function checkDuration(){
            return(file_exists($this->loc."/".$this->file) && (time() - $this->duration < filemtime($this->loc."/".$this->file))? true : false); 
        }

        public function useCache(){

         if(self::checkDuration()){
             ob_start( );
        include( $this->loc."/".$this->file);
        $this->output = ob_get_clean( );

        }
            else{
                ob_start(); 
        ## include the page to be cached
        include ($this->content);

        ## prepare for writing the cache file
        $filePointer = fopen($this->loc."/".$this->file, 'w');

        ## grab the output buffer and write it in cachefile
        fwrite($filePointer, ob_get_contents());

        ## close the file
        fclose($filePointer);

        ## Send the output to the browser
         $this->output = ob_end_flush();
            }

            return $this->output;
        }

    }
    ## end of class
?>

You can call this class from any page..for example

<?php

include_once 'cache.class.php';

## instantiate the cache class

$showCache = new MakeCache("reporst.php", 10080 * 60, "cache","reports.html");

## print or echo will just work equally fine, but for this output I prefer the print function
print $showCache->useCache();

?>

If you need to generate cache of antoher part of the page like the content for the right column, you can do so by just instantiating the class and print the method useCache.

for example,

        $rightColCache = new MakeCache("rightCol.php", 10080 * 60, "cache","rightCol.html");

        print $redColCache->useCache();

I was going to add a compression methods, but for zome reason I forgot how to implement the gzip function, and I am just too lazy to look it up. Ouch.. my age is getting on me now...how in the world I can possibly forgot about that..??

Thanks a ton Veedeoo, its works just fine! Now what I am wondering about is - Do I have to open the reports.php file in a browser and then run the code right away (i am using your first solution) ? what happens if there is already a cached page but has not been updated. Is there a way which will allow me to load up reports.php and then run your code so that I dont have to worry about the different versions of the cached files ?

I really appreciate your time and help here :)

I think i figured it out. I deleted the following from your first solution.

 if(file_exists($cachefile) && (time() - $cachetime < filemtime($cachefile)))  {
     ## the page has been cached from an earlier request
     ## output the contents of the cache file
     include($cachefile); 
     }
 else{

Now, if there is already a cached file then the code would just overwrite it with a new one.

Please let me know if I am doing anything wrong here.

@ veedeoo, What editor are you using for php programming? Any good editor for cakephp function autocompletion?

@rotten69,

I am using many different editors depending on what I have to work on. For instance, the above scripts I have provided in this thread was written on browser based editor, using my own php editor class in my portable xampp. I wrote this simple php editor class, so that I can write and test codes right away without the hassle of typing the url just to access the file. What is so cool about this php editor class? I can edit my php codes in browser environment.

For full php developement regardless of framework use e.g. code Igniter, cake php, and doophp , I normally use netbeans IDE php edition. I use it because of the smarty templating plugin for it. NetBeans IDE also have an auto error checking that can make the development process a lot easier. On top of this, I am using my own function and class finder just to make sure I can find the exact page where the functions or classes has been used or instantiated. The Netbean built-in search function is kind of slow, so my solution is to write my own search script that is fast enough to find functions, variables, class in all the files within specified directory.

For android related apps and simple Java, I use eclipse.

For simple and fast coding I use either notepad++ or PHP designer 2007 personal free edition. For the initial writing of the codes, I use the notepad++ first and then to debug the codes I load it in the php designer.

PHP designer 2007 have an auto completion for quotes and brackets that's all, but I never like it for writing initial script, because the auto completion bothers me a lot. If you are like me who changes the codes a lot while the developement progresses, you don't want the auto-completion features getting ahead of what you are trying to do. I love auto completion in html development though, because I want it go auto close html tags.

NOtepad++ I use this a lot because I like the sytax highlight function and there are many style templates to choose from.

On top of those editors and IDE, I also downloaded the php documentation from php.net, and unzipped it in my portable xampp. Normally, I would read it when not busy just to get familiar with php functions I have not use or encountered.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.