Back to File class

Method makeSafe

public static string
makeSafe
(mixed $file)
Makes file name safe to use
Parameters
  • string $file The name of the file [not full path]
Returns
  • string The sanitised string
Since
  • 1.7.0
Class: File
Project: Joomla

Method makeSafe - Source code

/**
 * Makes file name safe to use
 *
 * @param   string  $file  The name of the file [not full path]
 *
 * @return  string  The sanitised string
 *
 * @since   1.7.0
 */
public static function makeSafe($file)
{
    // Remove any trailing dots, as those aren't ever valid file names.
    $file = rtrim($file, '.');
    // Try transliterating the file name using the native php function
    if (function_exists('transliterator_transliterate') && function_exists('iconv')) {
        // Using iconv to ignore characters that can't be transliterated
        $file = iconv("UTF-8", "ASCII//TRANSLIT//IGNORE", transliterator_transliterate('Any-Latin; Latin-ASCII', $file));
    }
    $regex = array('#(\\.){2,}#', '#[^A-Za-z0-9\\.\\_\\- ]#', '#^\\.#');
    return trim(preg_replace($regex, '', $file));
}