Showing Filesize

From phpCMS

Jump to: navigation, search

Contents

[edit] Determine and print file sizes using PAXPHP and PAXTAGS

[edit] Introduction

If you offer files to be downloaded, it's usually wise to give the file size as well.

As it is very troublesome to determine the fileseize for each file manually, it's very helpful to have an automated solution for this. On static HTML pages you could for example use SHTML for this, and in phpCMS you've got PAXTAGS.

The tag we'll be using here is used like this:

<!-- PAXFSIZE file="filename.download" -->

Unfortunately, this tag is not (yet) part of the phpCMS code, so we need to add it first.

[edit] The tag PAXFSIZE

[edit] Adding the tag

In order to add the tag to the PAXPHP class, you have to edit the file class.pax_phpcms.php and insert the function _setPAXFSIZE:

class.pax_phpcms.php:


/*********************************************
   fsize for files
*********************************************/
function _setPAXFSIZE($contentfile)
{
  global $DEFAULTS, $PAGE;

  // Determine replacement text
  $no_such_file = "PAXFSIZE_FileNotFound";
  foreach ($PAGE->tagfile->tags as $tag)
    if ($tag[0] == $no_such_file)
    {
      $no_such_file = $tag[1];
      break;
    }

  // Iterate over content
  $countcontentfile = count($contentfile);
  for($contcount = 0; $contcount < $countcontentfile; $contcount++)
  {
    if(preg_match_all('/<!-- PAXFSIZE file="([a-zA-Z0-9_+-.\/]+)" -->/', $contentfile[$contcount], $matches))
    {
      foreach($matches[1] as $fname)
      {
        $full_path = $DEFAULTS->DOCUMENT_ROOT."/".$fname;
        if(!file_exists($full_path))
        {
          $fsize = $no_such_file;
        }
        else
        {
          $size =  filesize($full_path);
          $fsize = ($size > 512)
                    ? (($size /1024 > 512)
                        ? sprintf("%.02f MB",($size /1024)/1024)
                        : sprintf("%.02f kB",$size /1024)
                      )
                    : sprintf("%d B",$size);
        }

        $contentfile[$contcount] = str_replace('<!-- PAXFSIZE file="'.$fname.'" -->', $fsize, $contentfile[$contcount]);
      }
    }
  }
  return $contentfile;
}

This new function now has to be called from the function setPAXTAGS:

/*********************************************
   replace all PAXTAGS with their values
*********************************************/
function setPAXTAGS(&$template) {
     _getPAXMENU();
     $template = _setPAXINC($template);
     $template = _setPAXRTF($template);
     $template = _setPAXHIGHLIGHT($template);
     $template = _setPAXMENU($template);
     $template = _setPAXCOMBO($template);
     $template = _setPAXCOMBOALL($template);
     $template = _setPAXLASTMOD($template);

     $template = _setPAXFSIZE($template);

     $template =  _change_phpCMS_TAGS($template);
     return $template;
 }

[edit] Explainin the tag functionality

The function _setPAXFSIZE scans the content-page line by line for the PAXFSIZE tag, using a regular expression.

The function preg_match_all returns all found PAXFSIZE tags of a line in an array, which then gets evaluated in a 'for' loop. The regular expression makes sure only to return the content of the file-parameter, which eases further evaluation.

Within the 'for'-loop, the function checks if the file exists at all. If so, it's file size is determined and formatted in a human-readable form using sprintf.

The original PAXFSIZE tag now gets replaced by the thus determined file size.

[edit] Using the tag

<!-- PAXFSIZE file="/downloads/welcome.pdf" -->

The file path has to be relative to the "document root"; in case the file is not found, the function prints PAXFSIZE_FileNotFound.

[edit] Configuring the error message

In order to keep the function language-independent, it returns PAXFSIZE_FileNotFound in case a file is not found. Now, this message is not really nice, so if you want to give your users a better one, just add a line to your tag-file:

 PAXFSIZE_FileNotFound := File not found

[edit] Remark

Please note that PAXTAGS are static - that means, if you got caching switched on, changes will only be shown in the calling file either after you modify the file (thus forcing a cache update) or deleting the cached version. Additionally, it's of course important to have PAXPHP and PAXTAGS activated if you want this function to be used.

Main Page: Tutorials MainPage | Top Page: MiniHowTos MainPage

Diese Seite in anderen Sprachen: Deutsch

Personal tools