What character sequence should I not allow in a filename?

后端 未结 6 1919
别跟我提以往
别跟我提以往 2020-12-06 04:55

I found out after testing that linux allows any character in a file name except for / and null (\\0). So what sequence should I not allow in a file

6条回答
  •  爱一瞬间的悲伤
    2020-12-06 05:23

    I would leave the determination of what's "valid" up to the OS and filesystem driver. Let the user type whatever they want, and pass it on. Handle errors from the OS in an appropriate manner. The exception is I think it's reasonable to strip leading and trailing spaces. If people want to create filenames with embedded spaces or leading dashes or question marks, and their chosen filesystem allows it, it shouldn't be up to you to try to prevent them.

    It's possible to mount different filesystems at different mount points (or drives in Windows) that have different rules regarding legal characters in a file name. Handling this sort of thing inside your application will be much more work than is necessary, because the OS will already do it for you.

提交回复
热议问题