Archive for August, 2006

21st Aug 2006

PHP Ajax File Browser

New Version: Ajax File Browser 2.0 Beta, this version is no longer supported.

As the new buzz technology is developing, I realised I had no real evidence that I infact know how to use it. Coupled with the need for an intranet file server application, I decided to make an Ajax version of a Server File Browser.

Initial Screen Using the Sarissa ajax library, this handy application only has a page weight of 71kb! Directory listings are limited to 30 files/folders per page, so the ajax requests are quite quick. Additionally, the application has been tested on Firefox and IE 6 only. Please leave me a comment if you have bugs with other browsers.

All shared files can be downloaded, and are force saved. Meaning that you can even share your PHP source files, without letting them be compiled first.

Application Configuration

After downloading the application, unzip the contents to a directory on your webserver. This application has been designed for a host where the php document root is the root path of the application. So, it cannot work under a sub-directory of a domain or host name. You may want to look into sub-domains if this affects you.

The application requires no database (although may do so in future, to log downloads etc). There is only one configuration file, config.inc.php. In this file there are two main settings:File Browsing

  1. Shares
    Paths to directories on the server. They do not have to exist as a sub-directory of the application, they can exist relatively or absolutely. So long as the web service has permissions to the directory, you can browse it. This is the biggest reason why I had to write my own application. Others on the net simply didn’t allow complete server browsing. You must be careful not to share anything crucial or anything that will compromise server security. Once a directory is shared, all sub-directories will also be shared.
  2. Blacklists
    You can exclude certain directory names, file names, and even file extensions from being viewed. I was also going to include something about hidden or system files aswell, but I got lazy :)

Apache vs Other Servers

Included in this package is a .htaccess file for Apache servers. It’s main directive is to prevent PHP’s short open tags. If you have an error with the htaccess file (as some servers will not allow php values in them) then you will have to make arrangements to prevent the short open tag in another way. The short open tag will affect the XML documents returned with the Ajax calls. This directive is now irrelevant in v1.1

Demo and Download

Changelog

  • 2006-09-15 v1.1, No need for short_open_tag anymore

Technorati: ajax, php, javascript, web 2.0

Posted in Ajax File Browser | 34 Comments »

09th Aug 2006

Easy PHP Debugging

Wether I’m making complex web applications or a five page no brainer site, if I’m using PHP to do something the chances are something’s not going to work out exactly to plan. I quite often find myself spitting out this bit of code to see the contents of my variables: print_r($var);

While this works very well, it can be daunting to look at without some sort of formatting. Another dilemma I’ve found (only on those big projects) is that I can’t remember where I’ve put that piece of code in order to remove or comment it out.

Today I spent a short amount of time making my life easier. I’ve created a function that works very much the same way, but unlike print_r, this function can handle any type of variable. It’s extremely simple to use. Once the function is included to your application, just call it: echo dba($var1,$var2,$var3…);

The function will handle any amount of variables you throw at it. Here’s a sample output from one of my sites:

================================================================================
Debug Called:
/home/vhosts/trade/www/sub-contractors/materials.php on Line 448
/home/vhosts/trade/www/sub-contractors.php on Line 109

================================================================================
Argument #1: String
Hello World!
End Argument #1
================================================================================
Argument #2: Integer
9316729
End Argument #2
================================================================================
Argument #3: Float
4582.55
End Argument #3
================================================================================
Argument #4: Array
(
[0]
=> foo
[1] => bar
)
End Argument #4
================================================================================
Argument #5: db_mysql Object
(
[dbname:private] => trade
[id_link:private] => Resource id #15
[eh] =>
[query_counter] => 8
)
End Argument #5
================================================================================

Notice that the first part explains the trace of pages called before this function. On the first entry and line number you’ll find this function’s call. Subsequent pages are ‘parents’ of this page.

It’s great! Where do I get it?

Technorati: php, programming

Posted in PHP | 3 Comments »

03rd Aug 2006

Custom Smarty Block Function: Loop

For those who haven’t heard of the Smarty Template Engine for PHP, it’s something you should look into. Of course, I fully agree with it’s designers who state that the system is designed for larger scale applications instead of five page websites.

One of the best features (apart from useful documentation) is the ability to include custom functions to the system. One of the features I’ve been missing is a Loop function similar to PHP’s for loop. So I’ve created one:

<?php
/*
* Smarty plugin
* -------------------------------------------------------------
* File:     block.loop.php
* Type:     block function
* Name:     loop
* Purpose:  loops over a piece of template content x amount of times
* -------------------------------------------------------------
*/
function smarty_block_loop($params, $content, &$smarty) {
  //check if the block has data
  if (is_null($content)) {
    return;
  }
  //check if the start parameter is set
  if (strlen($params['start']) == 0 || !is_numeric($params['end'])) {
    $smarty->trigger_error("assign: missing 'start' parameter or is ".
      "not a number");
    return;
  }
  //check if the end parameter is set
  if (strlen($params['end']) == 0 || !is_numeric($params['end'])) {
    $smarty->trigger_error("assign: missing 'end' parameter or is ".
      "not a number");
    return;
  }
  //check if the increment is set. If not, default is +1
  if (empty($params['increment']) || !is_numeric($params['increment'])) {
    $params['increment'] = 1;
  }
 
  //do the loop!
  $data = '';
  //check if we're incrementing or decrementing
  if ($params['end'] > $params['start']) {
    //incrementing, check $increment value is positive
    if ($params['increment'] <= 0) {
      $smarty->trigger_error("assign: increment cannot be negative or ".
        "zero when 'start' is less than 'end'");
      return;
    }
    for ($x=$params['start'];$x < $params['end'];$x=$x+$params['increment']) {
      $data .= $content;
    }
  } else {
    //decrementing, check $increment value is negative
    if ($params['increment'] <= 0) {
      $smarty->trigger_error("assign: increment cannot be positive or ".
        "zero when 'start' is greater than 'end'");
      return;
    }
    for ($x=$params['start'];$x > $params['end'];$x=$x+$params['increment']) {
      $data .= $content;
    }
  }
  //return the data
  return $data;
} //end smarty_block_loop
?>

Download this code: block.loop.php

To use this in your Smarty engine, save this file to the plugins directory. It will automatically be available to you in your scripts. Here’s an example of it’s use:

<p>Smarty loop block function example</p>
<p>Assume that $blocks is defined as 10</p>
{loop start=0 end=$blocks increment=2}
  <div>
    Hello World
  </div>
{/loop}
<p>There should be 5 divs displayed.</p>

Download this code: block.loop.example.html

The increment value is not always required. It defaults to 1 if not specified. Also, this loop code works in reverse. Specifying a start value greater than the end value will cause the loop to decrement. Although, the increment value is required in such case and must be a negative value.

Technorati: php, smarty

Posted in PHP | No Comments »