是的,这是一个难题,因为该命令使用了非标准的 shell 参数(就像它自己的小元语言一样)。ImageMagick 也有同样的问题。
如果您只是在双引号字符串中使用 escapeshellarg(),它就会变得毫无用处。escapeshellcmd() 会转义所有特殊字符,并且在双引号字符串中使用是安全的。因此,您需要在其周围硬编码单引号以使其正常工作。
exec('/usr/local/bin/exiv2 -M"set Iptc.Application2.Caption String \'' . escapeshellcmd($caption) . '\'" modify IMG.jpg');
escapeshellarg() 在单引号字符串中不起作用的原因是:
# for this input:
The smith's "; rm -rf *; echo "went to town
# after escapeshellarg()
'The smith\'s "; rm -rf *; echo "went to town'
# Works fine if left as a top-level argument
/usr/bin/command 'The smith\'s "; rm -rf *; echo "went to town'
# BUT if put in a double-quoted string:
/usr/bin/command "subcommand1 'The smith\'s "; rm -rf *; echo "went to town'"
# it is broken into 3 shell commands:
/usr/bin/command "something and 'The smith\'s ";
rm -rf *;
echo "went to town'"
# bad day...