【发布时间】:2009-06-04 09:44:56
【问题描述】:
我目前实现这个的还不错的版本是:
function bashFileConvert($file)
{
return preg_replace('/([^\/\s]+\s+[^\/]+)(\/|$)/','"${1}"${2}',$file);
}
主要处理文件名中有空格时的问题,比如
$flie = '/usr/local/my test file.txt'
while 将无法被 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"
似乎使用此功能,引号被替换为空格。
【问题讨论】: