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

34 Comments

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

3 Comments

External Winamp Remote Control

How cool is this? Would have come in handy a few years ago when I was designing software for a Digital Jukebox…

Pastelo’s Winamp Blog Article

No Comments

Adding and Stripping Slashes with Arrays in PHP

How often have you ever found out that magic quotes was turned on, but you forgot all about it? Or your result from a database row is fine in its’ result set, except for the slashes?

Not too often for me either. But there are exceptions. Today, for example, I needed to stripslashes from every string in an array of strings. The normal stripslashes function just isn’t equipped to handle arrays of data.

Here’s what I’ve come up with:

<?php
function stripslashesArray($arr) {
  //check if the variable is an array
  if (is_array($arr)) {
    //get the keys of the array
    $keys = array_keys($arr);
    for ($x=0;$x<count($keys);$x++) {
      //check if array item is not an object
      if (!is_object($arr[$keys[$x]])) {
        //strip the slashes of the item, even if it's an array
        $arr[$keys[$x]] = stripslashesArray($arr[$keys[$x]]);
      }
    }
    return $arr; //return the stripped array
  } else {
    //variable is just a string, treat as normal
    return stripslashes($arr); //return the stripped string
  }
} //end stripslashesArray function
?>

Download this code: stripslashesarray.php

So you see, the function makes sure you’re not sending it an object, or an array with an object as an item. The function also calls itself again and again until all branches on the array are stripped. However, if you have an array with an object as an item, and that object has an array of strings, they won’t be stripped. I don’t see many opportunities where that would come up anyway!

Now that we’re stripping slashes, it’s just a simple modification to start adding slashes:

<?php
function addslashesArray($arr) {
  //check if the variable is an array
  if (is_array($arr)) {
    //get the keys of the array
    $keys = array_keys($arr);
    for ($x=0;$x<count($keys);$x++) {
      //check if array item is not an object
      if (!is_object($arr[$keys[$x]])) {
        //add the slashes to the item, even if it's an array
        $arr[$keys[$x]] = addslashesArray($arr[$keys[$x]]);
      }
    }
    return $arr; //return the stripped array
  } else {
    //variable is just a string, treat as normal
    return addslashes($arr); //return the stripped string
  }
} //end addslashesArray function
?>

Download this code: addslashesarray.php

The functions have more use than you’ve thought. For example, if you’ve just transferred your site to a new server, and magic quotes behave differently than before, you can use stripslashesArray or addslashesArray on your $_POST, $_REQUEST and $_GET arrays.

I hope this keeps you all sane.

Technorati: php, programming

No Comments

PHP Class: Paypal Button Generator

Ever tried to make a Paypal button using their Button Creator, or even by getting the information from their Help pages? It’s not easy or dynamic. I’ve made many sites that use Paypal for their payment of both single items and multi item shopping carts. I’ve created this class to make my job easier. I have not come upon something like this in my search on the internet, so I’m sharing it to the world.

This class supports:

  • CSS Styles
  • Custom IPN, Return, and Cancel pages
  • Postage, handling and tax amounts per item
  • Custom pass-through variable

NEW – Version 1.1 now supports PayPal Subscriptions!

Sites using this class:

Download

Technorati: php, paypal, ecommerce

24 Comments

PHP Class: Image Snapshot 1.3

Inspired by Backpackit.com’s image management, Image Snapshot can take a portion of an image (crop) to specific dimensions, from different areas of the original image.

This class has been created for the purpose of manipulating an uploaded image, and creating a Thumbnail image that does not alter the aspect ratio, and yet represents the contents of the original image. I’m sure other uses can be found for the Image Snapshot class, but I’ll leave that to you. I use it for Photo Galleries, and Shopping Cart systems.

View the manual to get a clearer picture.

This class supports:

  • Jpg, Gif, Png files
  • Output to Jpg
  • Can resize images before applying the crop
  • Can crop from any part of the image

Sites using this class:

Change Log
05-12-2005 1.3 Release
05-12-2005 Added: ImageFile field, can now load image from saved file.
05-12-2005 Added: custom position type, specify by percentages. Read updated manual for help with this.
28-11-2005 1.2 Release
28-11-2005 Added: ReturnedWidth and ReturnedHeight variables, for more feedback.
28-11-2005 Fixed: if crop area was bigger than original image, mixed results ocurred.
28-11-2005 1.1 Release
28-11-2005 Added: support for Image input with a variable
27-11-2005 1.0 Release

Downloads

Technorati: php, images, gd

20 Comments