问题描述
file_exists()
检查文件是否存在。
但是,在我的系统上,“ pg_ctl”是有效的命令,因为C:\PostgreSQL\bin
在我的PATH
中,因此运行“ pg_ctl”实际上会运行C:\PostgreSQL\bin\pg_ctl.exe
。
file_exists
将报告“ pg_ctl”不存在,但它仍然“ kinda”存在,因为它可以运行。
如何检查给定的路径或字符串(例如“ pg_ctl”)是否转换为有效命令?
我已经尝试过realpath
。它返回realpath('pg_ctl')
的布尔值false,因此不起作用。
解决方法
在Windows上,您可以使用where.exe
,如果路径中存在该文件,它将返回该文件的路径。
shell_exec('where pg_ctl');
应返回:
C:\ PostgreSQL \ bin \ pg_ctl.exe
因此请在输出中检查它:
$cmd = 'pg_ctl';
var_dump( strpos(shell_exec("where $cmd"),$cmd) !== false );