Removing Special Characters from uploaded file name in PHP

Uploaded file name is very vital to insert, update and retrieve media or other data. sometimes during uploading files filename contains some special characters (like $%^& etc.) which create complexity. that’s why we try to avoid special characters in our filename in the database. we can remove special characters from the uploaded filename by using a simple regular expression.

preg_replace(‘/[^a-zA-Z0-9_ -]/s’,”,$string);

If we want to keep some specific special character like “(  ) / . % – &” then we just need to change it as follows

preg_replace(‘/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s’,”,$string);    [It removes “(  ) / . % – &” from filename]


					
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s