LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / PHP Codes » How To Clean Or Sanitize String For File Name With Standards Using PHP?

How To Clean Or Sanitize String For File Name With Standards Using PHP?


Are you trying to come up with a function that does a good job of sanitizing certain strings so that they are safe to use in the URL (like a post slug) and also safe to use as file names? For example, when someone uploads a file I want to make sure that I remove all dangerous characters from the name.

Here is the explanation.

Strip HTML Tags:

  1. Remove Break/Tabs/Return Carriage
  2. Remove Illegal Chars for folder and filename
  3. Put the string in lower case
  4. Remove foreign accents such as Éàû by converting them into HTML entities and then remove the code and keep the letter.
  5. Replace Spaces with dashes
  6. Encode special chars that could pass the previous steps and enter in conflict filename on the server. ex. “中文百强网”
  7. Replace “%” with dashes to make sure the link of the file will not be rewritten by the browser when querying the file.

OK, some filenames will not be relevant but in most cases, it will work.

example;
Original Name: საბეჭდი-და-ტიპოგრაფიული.jpg
Output Name:-E1-83-A1-E1-83-90-E1-83-91-E1-83-94-E1-83-AD-E1-83-93-E1-83-98--E1-83-93-E1-83-90--E1-83-A2-E1-83-98-E1-83-9E-E1-83-9D-E1-83-92-E1-83-A0-E1-83-90-E1-83-A4-E1-83-98-E1-83-A3-E1-83-9A-E1-83-98.jpg

It’s better like that than a 404 error.

This is a simple frontend utility to help the file-upload process on your website. It is written in pure PHP, has no dependencies. You can check out the live demo. For the most part, browsers do a good job of handling image uploads. That being said – we find that the ability to show our users a preview of their upload can go a long way in increasing the confidence in their upload.

Table of Contents

How To Clear Your Text For File Name With Standards Using PHP?

There are a few easy and understandable steps to achieve your desired functionality using pure Vanilla JavaScript that we are gonna share below. Follow each step perfectly.

PHP:

/****************************************************************************/
// Clean The File Name
/****************************************************************************/
function normalizeFileNameString ($str = '')
{
//https://stackoverflow.com/a/19018736
$str = strip_tags($str);
$str = preg_replace('/[\r\n\t ]+/', ' ', $str);
$str = preg_replace('/[\"\*\/\:\<\>\?\'\|]+/', ' ', $str);
$str = strtolower($str);
$str = html_entity_decode( $str, ENT_QUOTES, "utf-8" );
$str = htmlentities($str, ENT_QUOTES, "utf-8");
$str = preg_replace("/(&)([a-z])([a-z]+;)/i", '$2', $str);
$str = str_replace(' ', '-', $str);
$str = rawurlencode($str);
$str = str_replace('%', '-', $str);
return $str;
}

Customization:

No need to customize it. Just copy-paste. Rest edit the code as per comments and need.

Troubleshooting the Errors

Do it with concentration and patience. Check your alls steps and again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.

Final Words:

That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us or If you have any doubts and problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave your precious feedback in our comment form below. Happy development, See you in the next article.

Recommended For You:
Simple JavaScript Text + Image Slider For Blog And Website

You Like It, Please Share This Recipe With Your Friends Using...

  

2 Responses to “How To Clean Or Sanitize String For File Name With Standards Using PHP?”

  1. konnecteum says:

    Really valuable blog. I have been following your blog for a long time. Keep up the good work and looking forward to more blogs 🙂

    • EXEIdeas says:

      Welcome here and thanks for reading our article and sharing your view. This will be very helpful to us to let us motivate to provide you with more awesome and valuable content from a different mind. Thanks again.

Leave a Reply

Your email address will not be published. Required fields are marked *