到目前为止,我实现这一点的不太糟糕的版本是:
function bashFileConvert($file)
{
return preg_replace('/([^\/\s]+\s+[^\/]+)(\/|$)/','"${1}"${2}',$file);
}
当文件名中有空格时,它主要处理问题,例如
$flie = '/usr/local/my test file.txt'
而 Bash 将无法识别。
所以我需要转换为
$file = '/usr/local/"my test file.txt"'
在调用类似的东西之前:
exec('ls ' . $file);
但是还有很多其他的极端情况,比如引号和 '&' 问题。
那么,是否有现成的版本来完成这项工作?
====================================
现在我尝试了 escapeshellarg(),但是这里有点奇怪:
$file = '/usr/local/apache2/resumes_txt/5/San Francisco/qtzhang/Device "Engineer"/Job Resume Qintao Zhang.pdf.txt';
echo escapeshellarg($file);
D:\\test>php test.php
"/usr/local/apache2/resumes_txt/5/San Francisco/qtzhang/Device Engineer /Job Resume Qintao Zhang.pdf.txt"
似乎使用此功能,引号被替换为空格。