public static function sanitize($fileName, $stripPath = true, $maxLength = 120, $defaultExtension = 'bin')
    {
        list($dirname, $name, $ext) = self::split($fileName);

        if (!$stripPath) {
            $unslashedFilename = str_replace('/', '-', ltrim($dirname, '/')) . '-' . $name . '.' . $ext;
            return self::sanitize($unslashedFilename, true, $maxLength);
        }

        if (!$ext) {
            $ext = $defaultExtension;
        }
        $maxBaselength = $maxLength - 1 - strlen($ext);
        if ($maxBaselength <= 0) {
            throw new \InvalidArgumentException("Maxlength of {$maxLength} results in an empty filename");
        }

        return substr(preg_replace('/[^\w.,-]/', '-', $name), 0, $maxBaselength) . '.' . $ext;
    }
public static function sanitize($fileName, $stripPath = true, $maxLength = 120, $defaultExtension = 'bin')
    {
        list($dirname, $name, $ext) = self::split($fileName);

        if (!$stripPath) {
            $unslashedFilename = str_replace('/', '-', ltrim($dirname, '/')) . '-' . $name . '.' . $ext;
            return self::sanitize($unslashedFilename, true, $maxLength);
        }

        if (!$ext) {
            $ext = $defaultExtension;
        }
        $maxBaselength = $maxLength - 1 - strlen($ext);
        if ($maxBaselength <= 0) {
            throw new \InvalidArgumentException("Maxlength of {$maxLength} results in an empty filename");
        }

        return substr(preg_replace('/[^\w.,-]/', '-', $name), 0, $maxBaselength) . '.' . $ext;
    }
public static function sanitize ( $ fileName , $ stripPath = true , $ maxLength = 120 , $ defaultExtension = 'bin' ) { list ( $ dirname , $ name , $ ext ) = self :: split ( $ fileName ) ; if ( ! $ stripPath ) { $ unslashedFilename = str_replace ( '/' , '-' , ltrim ( $ dirname , '/' ) ) . '-' . $ name . '.' . $ ext ; return self :: sanitize ( $ unslashedFilename , true , $ maxLength ) ; } if ( ! $ ext ) { $ ext = $ defaultExtension ; } $ maxBaselength = $ maxLength - 1 - strlen ( $ ext ) ; if ( $ maxBaselength <= 0 ) { throw new \ InvalidArgumentException ( "Maxlength of {$maxLength} results in an empty filename" ) ; } return substr ( preg_replace ( '/[^\w.,-]/' , '-' , $ name ) , 0 , $ maxBaselength ) . '.' . $ ext ; }
Sanitize a file name, excluding unwanted characters and replacing them with a dash.

@param string $fileName
@param bool $stripPath
@param int $maxLength
@param string $defaultExtension
@return string

@throws \InvalidArgumentException
Sanitize a file name excluding unwanted characters and replacing them with a dash .