In Web developing there are some main features and many concerns that we all must be aware. The file uploading is one of the important and indispensible features that we certainly will use sooner or later.

I’m sure that many of you have had troubles when uploading files that have names with special chars, especially if we run our application in a Linux SO. In fact, using Linux we have to be aware of case sensitive names and special chars. In Windows, we usually don’t have this problem and we can name our files without many limitations. This problem has a higher importance in Latin countries like mine (Portugal), here is normal to name files like “Activação Telefónica.doc”.

So, I decided to code a simple function that cleans the file name and post it here to be sure that when I need it again I known were this little simple but useful function is. I use the PHP function preg_replace to replace special chars with standard ones. This is only an example and don’t clean all special chars. You should add more chars to the filter by simple adding more arrays to the cleaner one. See the function bellow:


<?
$txt = "Activação Telefónica.jpg";

function cleanFileName( $str )
{
$cleaner = array();
$cleaner[] = array('expression'=>"/[àáäãâª]/",'replace'=>"a");
$cleaner[] = array('expression'=>"/[èéêë]/",'replace'=>"e");
$cleaner[] = array('expression'=>"/[ìíîï]/",'replace'=>"i");
$cleaner[] = array('expression'=>"/[òóõôö]/",'replace'=>"o");
$cleaner[] = array('expression'=>"/[ùúûü]/",'replace'=>"u");
$cleaner[] = array('expression'=>"/[ñ]/",'replace'=>"n");
$cleaner[] = array('expression'=>"/[ç]/",'replace'=>"c");

$str = strtolower($str);
$ext_point = strripos($str,”.”); // Changed to strripos to avoid issues with ‘.’ Thanks nico.
if ($ext_point===false) return false;
$ext = substr($str,$ext_point,strlen($str));
$str = substr($str,0,$ext_point);

foreach( $cleaner as $cv ) $str = preg_replace($cv[“expression”],$cv[“replace”],$str);

return preg_replace(“/[^a-z0-9-]/”,”_”,$str).$ext;
}

echo cleanFileName( $txt );
?>

I hope that this function will be useful to you.

5 Responses to “Clean file name in PHP with Regular Expressions”

admin

February 4th, 2009 - 17:19

This post meant to be a simple function to clean a filename, specially from latin chars that is a common problem in web servers.
If you have some question or suggestion be comfortable to make it.

Kays

June 5th, 2009 - 14:01

Nice function and just what I was looking for. However I noticed a problem when there are multiple periods (dots) in the file name. I would suggest using the following to separate the file extension.

nico_nico

July 6th, 2009 - 09:04

I’d suggest to use the strripos() function instead of stripos(). It will solve the multiple periods issue in the file name.

admin

July 6th, 2009 - 09:12

You’re totally right. I’ve already updated the function using strripos. Thanks.

games

September 30th, 2012 - 00:02

this is very nice way to screue upload file name
$file_name = trim(basename(stripslashes($name)), “.\x00..\x20”);

anyway thanks

Leave a Reply

Security Code:

Recent comments

Valid XHTML 1.0 Transitional Valid CSS!

Recent posts

2007-2014 © ProTech, All rights reserved | Powered by WS4.org