文件名的字符串清理器 [英] string sanitizer for filename
问题描述
我正在寻找一个php函数,该函数将清理字符串并使其可用于文件名.有人知道得心应手吗?
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 worried that I'll overlook a character! )
用于将文件保存在Windows NTFS文件系统上.
for saving files on a Windows NTFS filesystem.
推荐答案
不用担心忽略字符-如何使用您愿意使用的字符白名单?例如,您可以只允许良好的a-z
,0-9
,_
和单个句点(.
).显然,这比大多数文件系统要严格得多,但应该可以确保您的安全.
Instead of worrying about overlooking characters - how about using a whitelist of characters you are happy to be used? For example, you could allow just good ol' a-z
, 0-9
, _
, and a single instance of a period (.
). That's obviously more limiting than most filesystems, but should keep you safe.
这篇关于文件名的字符串清理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!