string sanitizer for filename

后端 未结 17 1300
长情又很酷
长情又很酷 2020-11-27 13:23

I\'m looking for a php function that will sanitize a string and make it ready to use for a filename. Anyone know of a handy one?

( I could write one, but I\'m worri

17条回答
  •  攒了一身酷
    2020-11-27 13:57

    Well, tempnam() will do it for you.

    http://us2.php.net/manual/en/function.tempnam.php

    but that creates an entirely new name.

    To sanitize an existing string just restrict what your users can enter and make it letters, numbers, period, hyphen and underscore then sanitize with a simple regex. Check what characters need to be escaped or you could get false positives.

    $sanitized = preg_replace('/[^a-zA-Z0-9\-\._]/','', $filename);
    

提交回复
热议问题