Sanitize filenames with PHP

PDF generation and invalid characters

While working with the TableTools PDF generator included in the great DataTables jQuery plugin I've noticed that sometimes the script failed to generate the PDF file. The cause? Invalid characters in the filename.

Well, to solve this issue I've made a simple PHP function to return a safe version of the given filename.

How it works?

Reads a filename string and replace each dangerous character with an underscore. Of course you can use any other safe character instead of an underscore.

The Code:

<?php

class Helper
{
    /**
     * Returns a safe filename, for a given platform (OS), by
     * replacing all dangerous characters with an underscore.
     *
     * @param string $dangerousFilename The source filename
     * to be "sanitized"
     * @param string $platform The target OS
     *
     * @return string A safe version of the input
     * filename
     */
    public static function sanitizeFileName($dangerousFilename, $platform = 'Unix')
    {
        if (in_array(strtolower($platform), array('unix', 'linux'))) {
            // our list of "dangerous characters", add/remove
            // characters if necessary
            $dangerousCharacters = array(" ", '"', "'", "&", "/", "\\", "?", "#");
        } else {
            // no OS matched? return the original filename then...
            return $dangerousFilename;
        }

        // every forbidden character is replace by an underscore
        return str_replace($dangerousCharacters, '_', $dangerousFilename);
        }
    }

Usage:

$safeFilename = Helper::sanitizeFileName('#my  unsaf&/file\name?"');

Download this and more...

This snippet and other useful [at least, I hope so] PHP methods are grouped on a git repo available via GitHub. This is also the best [and only] way to get the updated version of the code.

Go to the project's web page on GitHub or download it right way with (you must have Git installed first):

$ git clone git://github.com/noiselabs/noiselabs-php-toolkit.git