PHP:转义文件名为linux [英] PHP: escape filename as linux does
问题描述
我遇到了需要处理的用户上传的文件名的麻烦。当我尝试访问它们时,由于其中一些具有特殊字符,所使用的命令表示该文件未找到或类似。
I'm having troubles with filenames upload by users which I have to process. When I try to access them, because some of them have special characters, the command used says the file is not found or similar.
我已经使用escapeshellcmd没有成功
I've used escapeshellcmd with no sucess.
当我在linux控制台中使用标签键(当你已经开始键入文件名并且想要完成它)时,bash正确地转义文件名,如果我使用的是escaped文件名,它可以工作。
When I use the "tab" key in linux console (when you have started to type the filename and you want it to complete), the bash escape the filename correctly, and if I use exactly that "escaped" filename, it works.
我已经尝试过:
preg_replace("/[^a-zA-Z0-9\-\.\s]/", "\\\\$0", $filename)
以避免数字,字母,和空格之外的所有东西,但是我发现文件test_1.jpg,这个命令将其转换成test_1.jpg,它不起作用,因为_不需要转换。
to escape everything except numbers, letters, - and spaces ... but I found that for file "test_1.jpg", this command converts it into "test_1.jpg", and it does not work, since "_" does NOT need to be converted.
我恐怕可以有更多的允许字符,所以我的问题是...我如何克隆Linux控制台中的tab键的转义功能bash?
I'm afraid there could be more "allowed" characters, so my question is ... how can i "clone" the escape function of "tab" key in linux console bash ?
谢谢!
推荐答案
如果要使用文件名作为shell命令参数,使用 escapeshellarg 功能。
If you want to use the file name as a shell command parameter you can use the function escapeshellarg.
这篇关于PHP:转义文件名为linux的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!