×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: Artur Słaboszewski
Added: Aug 1, 2017 8:53 AM
Modified: Oct 24, 2019 11:27 PM
Views: 2492
  1. <?php
  2.     /**
  3.      * Sanitize file upload file name
  4.      *
  5.      * @param string $filename <p>
  6.      * The input string
  7.      * </p>
  8.      * @return string
  9.      */
  10.     function sanitize_file_name( $filename ) {
  11.  
  12.         $pl_chars = Array(
  13.             //WIN
  14.             "\xb9"               => "a",
  15.             "\xa5"               => "A",
  16.             "\xe6"               => "c",
  17.             "\xc6"               => "C",
  18.             "\xea"               => "e",
  19.             "\xca"               => "E",
  20.             "\xb3"               => "l",
  21.             "\xa3"               => "L",
  22.             "\xf3"               => "o",
  23.             "\xd3"               => "O",
  24.             "\x9c"               => "s",
  25.             "\x8c"               => "S",
  26.             "\x9f"               => "z",
  27.             "\xaf"               => "Z",
  28.             "\xbf"               => "z",
  29.             "\xac"               => "Z",
  30.             "\xf1"               => "n",
  31.             "\xd1"               => "N",
  32.             //UTF
  33.             "\xc4\x85"   => "a",
  34.             "\xc4\x84"   => "A",
  35.             "\xc4\x87"   => "c",
  36.             "\xc4\x86"   => "C",
  37.             "\xc4\x99"   => "e",
  38.             "\xc4\x98"   => "E",
  39.             "\xc5\x82"   => "l",
  40.             "\xc5\x81"   => "L",
  41.             "\xc3\xb3"   => "o",
  42.             "\xc3\x93"   => "O",
  43.             "\xc5\x9b"   => "s",
  44.             "\xc5\x9a"   => "S",
  45.             "\xc5\xbc"   => "z",
  46.             "\xc5\xbb"   => "Z",
  47.             "\xc5\xba"   => "z",
  48.             "\xc5\xb9"   => "Z",
  49.             "\xc5\x84"   => "n",
  50.             "\xc5\x83"   => "N",
  51.             //ISO
  52.             "\xb1"               => "a",
  53.             "\xa1"               => "A",
  54.             "\xe6"               => "c",
  55.             "\xc6"               => "C",
  56.             "\xea"               => "e",
  57.             "\xca"               => "E",
  58.             "\xb3"               => "l",
  59.             "\xa3"               => "L",
  60.             "\xf3"               => "o",
  61.             "\xd3"               => "O",
  62.             "\xb6"               => "s",
  63.             "\xa6"               => "S",
  64.             "\xbc"               => "z",
  65.             "\xac"               => "Z",
  66.             "\xbf"               => "z",
  67.             "\xaf"               => "Z",
  68.             "\xf1"               => "n",
  69.             "\xd1"               => "N" );
  70.  
  71.         $filename = strtr( $filename, $pl_chars );
  72.  
  73.         $filename = trim( $filename );
  74.         $filename = preg_replace( '/[ ]+/', '_', $filename );
  75.         $filename = preg_replace( '/[–]+/', '-', $filename );
  76.         $filename = preg_replace( '/\.(?=.*\.)/', '', $filename ); // remove all dots but not last dot
  77.         $filename = preg_replace( '/[^_\-0-9a-zA-Z]+/', '-', $filename );
  78.         $filename = preg_replace( '/[-\-]+/', '-', $filename );
  79.         $filename = preg_replace( '/[__]+/', '_', $filename );
  80.         $filename = rawurlencode( $filename );
  81.         $filename = strtolower( $filename );
  82.  
  83.         return $filename;
  84.  
  85.     }
  86.  
  87.         // Other function for sanitize filename
  88.     function sanitizeFileName($strFilename) {
  89.                 $badCharacters = ["~", "`", "!", "?", "@", "#", "$", "%", "^", "&", "*", " ", '"', "'", "/", "\\", "[", "]", "(", ")"];
  90.                 return trim(str_replace($badCharacters, '_', $strFilename), '_');
  91.         }
  92.  
  93. ?>