- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我编写了一个使用 exec() 的 Web 应用程序为了运行外部程序。程序路径是可配置的,并且可以预期其名称上有空格。众所周知,Windows 命令提示符接受文件名或参数中的空格,您只需将它们加双引号即可:
C:\>C:\Archivos de programa\GraphicsMagick-1.3.12-Q16\gm.exe
"C:\Archivos" no se reconoce como un comando interno o externo,
programa o archivo por lotes ejecutable.
C:\>"C:\Archivos de programa\GraphicsMagick-1.3.12-Q16\gm.exe"
GraphicsMagick 1.3.12 2010-03-08 Q16 http://www.GraphicsMagick.org/
到目前为止一切顺利。我面临的问题是 exec() PHP 函数本身的用法。某些 Windows 服务器要求您将完整的命令 (program + arguments) 用双引号引起来:
exec('""C:\Archivos de programa\GraphicsMagick-1.3.12-Q16\gm.exe" version"');
...和其他 Windows 服务器要求不使用双引号:
exec('"C:\Archivos de programa\GraphicsMagick-1.3.12-Q16\gm.exe" version');
我可以读取 PHP_OS
常量来检测服务器是否运行 Windows,但我不知道引号或不引号主题背后的规则是什么。如果在 PHP 手册中有解释,我找不到它。
是否可以通过编程方式确定是否需要引号,这样我就不需要手动配置应用程序的每个实例?
更新#1:我被误解了,所以我改写了部分问题以使其更清楚。
更新 #2: 我找到了 a comment in the PHP manual这解释了为什么需要额外引号的确切原因(PHP 发出对 cmd/c
的内部调用)。我仍然不知道为什么这看起来是对还是错取决于系统。
最佳答案
我写了这个快速而肮脏的解决方法:
<?php
class Thumb{
const GM_PATH = 'C:\\Archivos de programa\\GraphicsMagick-1.3.12-Q16\\gm.exe';
/**
* Quote full command if required by server (program + arguments)
*/
private static function quoteFullCommand($command){
// Test only once per script
static $extra_quotes_required=NULL;
if( is_null($extra_quotes_required) ){
if(PHP_OS=='WINNT'){
// This call will be correct (0) if and only if the server requires extra quotes
exec('""sort" /?"', $output, $return);
$extra_quotes_required = $return==0;
}else{
$extra_quotes_required = FALSE;
}
}
if($extra_quotes_required){
$command = '"' . $command . '"';
}
return $command;
}
/**
* Return output from "gm version"
*/
public static function graphicsMagickVersion(){
$command = escapeshellarg(self::GM_PATH) . ' version ';
$command = self::quoteFullCommand($command);
exec($command, $output, $return);
return trim(implode(PHP_EOL, $output));
}
}
但是,最好从 PHP 版本或服务器操作系统来预测它,因此欢迎提供文档链接或更多提示。
更新:我看过一段 PHP 源代码,负责在 Windows 中运行外部命令:
http://svn.php.net/viewvc/php/php-src/trunk/TSRM/tsrm_win32.c
以下行将额外的逗号添加到完整命令中:
sprintf(cmd, "%s /c \"%s\"", TWG(comspec), command);
根据文件历史记录,该行最早添加于 2008 年 5 月 29 日 (r260429):
MFH: Fix bug when command is quoted and parameters are quoted during call to exec, the result is that cmd.exe /c strips the first and last quote.
以下 PHP 版本是 5.3.0 和 5.2.7,但该行位于 5_3 分支而不是 5_2 分支。我对 PHP 开发过程不够熟悉,所以我找不到变更日志或说出修复程序移植到哪个确切的 PHP 版本,但我敢说这是 PHP 中的错误并且已在 PHP 中修复/5.3.0(但它没有被移植到 5.2,所以他们没有破坏遗留的东西)。
所以我的解决方法可能有点矫枉过正。您只需要测试 PHP 操作系统和版本:
if( PHP_OS=='WINNT' && version_compare(PHP_VERSION, '5.3.0', '<') ){
$command = $command = '"' . $command . '"';
}
关于php - 执行(): quoting full command in Windows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4255162/
我正在尝试安装一种用于 TCPDF 的字体。为此,我需要运行附带的命令行实用程序 ttf2ufm。 (包含在 TCPDF 中的 fonts/utils/ttf2ufm 中)但是当我运行它时,出现错误
我是 answering a question并假设 if [ $(command) ]; then ... 总是等同于 if command; then ... 但是,我得到了a couple of
我的package.json定义: ... "scripts": { "start": "node scripts/start.js", "build"
我知道默认的“CTRL+B”Windows 命令可用于显示所有嵌套文件。 是否有显示所有嵌套文件夹的快捷方式? 最佳答案 我怀疑 Total Commander 中是否存在此功能。内置tree实用性对
任何人都可以告诉我,是否有任何可以检测当前运营商名称的AT命令? 我用过AT+COPS? 它返回给我运算符(operator)的数字代码:0,2,40410 然后我使用命令 AT+WOPN = 0,4
我需要将网站托管到 google firebase 托管。 我几乎到处都看过,但钢有问题。 我已安装 npm install --global firebase 还有 npm install -g f
我想要这样的东西 如果(command_not_exists)退出 谁能告诉我如何在 cshell 脚本中实现此功能? 最佳答案 我的问题是使用 where 命令解决的(我尝试使用 which 命令)
通过使用 + 的参数调用它,我可以使 vim 将光标定位在文件的最后一行。 : vi + myfile # "+" = go to last line of file 我怎样才能做到
我想要这样的东西 如果(command_not_exists)退出 谁能告诉我如何在 cshell 脚本中实现此功能? 最佳答案 我的问题是使用 where 命令解决的(我尝试使用 which 命令)
在 cobra 中,我创建了一个命令命令: myapp zip -directory "xzy" -output="zipname" myapp upload -filename="abc" 我想制作
我应用了所有可能的答案,但仍然是同样的问题。 也试过 $this->db->reconnect(); 我的查询没有问题 我的代码: public function GetdistributorsDet
有什么区别: eval echo lala 和: command="echo lala" $command 它们似乎都具有相同的效果,但我可能遗漏了一些东西。此外,如果它们确实具有相同的效果,那么ev
要将命令的输出存储为 sh/ksh/bash 中的变量,您可以执行任一操作 var=$(command) 或 var=`command` 这两种方法有什么区别? 最佳答案 反引号/标记已被弃用,取而代
我想让我的用户根据他的选择以 sudo 和普通用户身份运行。他可以使用 sudo 或普通用户,但如果不使用 sudo,我必须禁用某些功能来消除错误。那么我怎么知道该用户是否给了我 sudo 执行权限?
这个问题已经有答案了: Command substitution: backticks or dollar sign / paren enclosed? [duplicate] (3 个回答) 已关闭
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: Command substitution: backticks or dollar sign / paren enc
要将命令的输出存储为 sh/ksh/bash 中的变量,您可以执行任一操作 var=$(command) 或 var=`command` 这两种方法有什么区别? 最佳答案 反引号/标记已被弃用,取而代
Linux 101 Hacks 这本书的第 38 页建议: cat url-list.txt | xargs wget –c 我通常这样做: for i in `cat url-list.txt`
问题是当我为我的项目编写 Makefile 时,当我需要检测当前分支名称时,在 make 规则中我这样做了: check_branch: if [ "$(git rev-parse --abb
默认情况下,控制台命令文件夹位于:MyProject\MyBundle\Command并且一切都按预期工作,但是如果我将文件夹移动到另一个目录中,例如:MyProject\MyBundle\MyFol
我是一名优秀的程序员,十分优秀!