Code
Being a professional Web Designer I often find myself needing (what I think are) simple standalone functions and, not wanting to reinvent the wheel, I hit Google. However of course there are always a few that I have to either invent myself from scratch or seriously bodge from another code snippet.
So in order to give something back to the general community of developers I call on, here are some of my most useful functions.
 
 
PHP - Display Random Image
I use this script to display a random avatar for my profile on a chatroom. Stick your images in a subfolder called
'avatars' and reference this script as if it were an image and you're away :)

$extList = array('gif' => 'image/gif','jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'png' => 'image/png');

if($handle = opendir(getcwd()."/avatars"))
{
    $pics = array();

    while (false !== ($file = readdir($handle)))
    {
        if($file != "." && $file != "..") array_push($pics, $file);
    }

    closedir($handle);

    $img = $pics[array_rand($pics)];

    $imageInfo = pathinfo($img);

    header ("'Content-type: ".$extList[$imageInfo['extension']]."'");
    @readfile("avatars/".$img);
}
 
 
PHP - Write a var_dump to a File
Using the output buffer to grab the var_dump output

ob_start();

var_dump($_REQUEST);

if ($handle = fopen("/tmp/error.log", "w"))
{
    fwrite($handle, ob_get_contents());
}

fclose($handle);
ob_end_clean();
 
 
PHP - Anti Spam Image for Forms
Reference this file as an image and give a text box for the code to be copied into. Then you can compare the submitted code
with the cookie that this will create.
Adapted from code written by the APLC team.

class Anti_Spam
{
    protected $width;
    protected $height;
    protected $fontfile;
    protected $code = '';
    
    public function __construct($width = 200, $height = 75, $chars = 6)
    {
        $this->width = $width;
        $this->height = $height;
        $this->fontfile = '/usr/share/fonts/TTF/arial.ttf';

        for($i=0; $i<$chars; $i++) $this->code .= chr(rand(65,90));
    }
    
    protected function imagelinethick($image, $x1, $y1, $x2, $y2, $color, $thick = 1)
    {
        if ($thick = = 1)
        {
            return imageline($image, $x1, $y1, $x2, $y2, $color);
        }

        $t = $thick / 2 - 0.5;

        if ($x1 = = $x2 || $y1 = = $y2)
        {
            return imagefilledrectangle($image, round(min($x1, $x2) - $t), round(min($y1, $y2) - $t),
round(max($x1, $x2) + $t), round(max($y1, $y2) + $t), $color);
        }

        $k = ($y2 - $y1) / ($x2 - $x1); //y = kx + q
        $a = $t / sqrt(1 + pow($k, 2));

        $points = array(
            round($x1 - (1+$k)*$a), round($y1 + (1-$k)*$a),
            round($x1 - (1-$k)*$a), round($y1 - (1+$k)*$a),
            round($x2 + (1+$k)*$a), round($y2 - (1-$k)*$a),
            round($x2 + (1-$k)*$a), round($y2 + (1+$k)*$a),
        );

        imagefilledpolygon($image, $points, 4, $color);

        return imagepolygon($image, $points, 4, $color);
    }

    public function spit()
    {
        $im = imagecreate($this->width, $this->height);
        imageantialias($im,1);
        
        $white = imagecolorallocate($im, 255, 255, 255);
        $black = imagecolorallocate($im, 0, 0, 0);

        for($i=0;$i<=2;$i++)
        {
            $this->imagelinethick($im, rand(0, $this->width), rand(0,$this->height), rand($this->width/2,$this->width),
rand($this->height/2,$this->height), $black, rand(0,3));
        }
        
        for($i=0,$x=10;$i<strlen($this->code);$i++,$x+=30)
        {
            imagefttext($im,rand(18, 26), rand(-35,35),
            $x, $this->height-rand(10,35), $black, $this->fontfile, $this->code[$i],array());
        }
        
        imagefilter($im, IMG_FILTER_MEAN_REMOVAL);
        imagecolortransparent($im, NULL);

        header ("'Content-type: image/png'");
        imagePNG($im);
    }

    public function getCode()
    {
        return $this->code;
    }
}

$antiSpam = new Anti_Spam();
setCookie("anti_spam_code", $antiSpam->getCode(), time()+60, "/");
$antiSpam->spit();
 
 
Java - Store System Properties
Store the system properties in an XML file

import java.io.*;

/**
* @param fileName the file to save the properties in
*/
public void storeSystemProperties(String fileName)
{
    try
    {
        Properties prop = System.getProperties();
    
        FileOutputStream fileOut = new FileOutputStream(new File(fileName));
        
        prop.storeToXML(fileOut, "Properties");
        fileOut.flush();
        fileOut.close();
    }
    catch(FileNotFoundException e)
    {
        System.out.println(e.getMessage());
    }
    catch(IOException e)
    {
        System.out.println(e.getMessage());
    }
}
 
 
Java - Parse a Configuration File
Read a config file, parse it and add key/value pairs to a hashmap

import java.io.*;
import java.util.HashMap;

/**
* @param configFile the location of the configuration file
* @return the produced HashMap
*/
public HashMap<String, String> irisReport(String configFile)
{
    HashMap<String, String> config = new HashMap<String, String>;

    try
    {
        BufferedReader bufRead = new BufferedReader(new FileReader(configFile));

        while(bufRead.ready())
        {
            String line = bufRead.readLine();

            if(!line.startsWith("#") && line.indexOf("=") != -1)
            {
                //Trim out whitespace, explode at equals and put key/value pairs into the config HashMap
                int splitPos = line.indexOf("=");
                String key = line.substring(0, splitPos).trim();
                String value = line.substring(splitPos+1, line.length()).trim();

                if(!key.equals("") && !value.equals(""))
                {
                    config.put(key, value);
                }
            }
        }

        bufRead.close();
    }
    catch (FileNotFoundException e)
    {
        System.out.println(e.getMessage());
    }
    catch (IOException e)
    {
        System.out.println(e.getMessage());
    }

    return config;
}
 
 
Java - Create MD5 Hash on a File
Pass it a filename and it'll pass you back the Hash. Simple as that!

import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public String getMD5Hash(String fileName)
{
    String hash = "";

    try
    {
        File file = new File(fileName);
        FileReader fileRead = new FileReader(file);
        MessageDigest md = MessageDigest.getInstance("MD5");

        char[] chars = new char[1024];
        int charsRead = 0;
    
        do
        {
            charsRead = fileRead.read(chars, 0, chars.length);

            if(charsRead >= 0)
            {
                md.update(new String(chars, 0, charsRead).getBytes());
            }
        }
        while(charsRead >= 0);

        hash = new BigInteger(1, md.digest()).toString(16);
    }
    catch (NoSuchAlgorithmException e){}
    catch (IOException e){}
        
    return hash;
}
 
 
PHP - Recursively Including Classes
Recurses down a directory tree and includes all the files it finds.

function autoload($dir)
{
    if($handle = opendir($dir))
    {
        $dirs = array();
    
        while (false !== ($file = readdir($handle)))
        {
            if($file{0} != ".")
            {
                if(!is_dir($dir.'/'.$file)) require_once $dir.'/'.$file;
                else array_push($dirs, $dir.'/'.$file);
            }
        }
    
        closedir($handle);
    
        while(count($dirs) > 0)
        {
            $this->autoload(array_pop($dirs));
        }
    }
}
 
 
Using Mod Rewrite to Make Pretty URLs the Safe Way
I know many many sites out there have tutorials on Mod Rewrite but I had to piece this together from a number of them
and it seemed like a fairly common thing to want to do.
If you're using the classic controller/action/id schema for your site and want to prettify your links so that the values can be
simply broken by slashes but you also want relative paths to any subfolders (for images, CSS etc) to work and you don't
want your subdomains to break then try this code in a .htaccess file in your web root.
Of course you may need to change the naming of your root file and arguments.

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -f                        [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d                        [NC] 
RewriteRule .* -                                          [L]

RewriteRule ^([^/]+)/?$ index.php?controller=$1                                    [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?controller=$1&action=$2                  [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/([0-9]+)/?$ index.php?controller=$1&action=$2&id=$3   [QSA,L]