function safe_filename ($fn, $len = 50)
Use this function to create a safe file name. A safe file name is a file name with only alphanumeric characters (A-Z, 0-9), non alphanumeric characters (including space) will be replaced with underscore '_'.
$fn = the original (non-safe) file name.
$len = maximum length of the name (excluding the extension).
Example 1
<?php
require './includes/user_init.php';
$fn = 'tHis is A long % file nam$ with. invalid ch#aracters.txt.doc';
echo safe_filename ($fn);
// output:
// this_is_a_long___file_nam__with__invalid_ch_aracte.doc
?>
Subscribe to our newsletter for the latest updates and exciting promotions!