PHP执行带参数外部程序无响应:exec调用失败排查求助
exec() Call for infekt-cmd.exe Return code 1 from exec() almost always indicates that the underlying command failed to run properly. Let's walk through the most likely issues and fixes:
1. Quote Paths with Spaces (Critical!)
The path to infekt-cmd.exe includes spaces (C:\Program Files\iNFekt\infekt\infekt-cmd.exe), which the Windows shell will split into separate arguments if not wrapped in quotes. The same goes for your $nfoFile path if it has spaces.
Update your command to wrap both paths in escaped double quotes:
$exePath = 'C:\\Program Files\\iNFekt\\infekt\\infekt-cmd.exe'; $nfoFile = 'C:\\path\\to\\your\\file.nfo'; // Replace with your actual path // Wrap executable and file paths in quotes to handle spaces $command = "\"$exePath\" -S --utf-16 \"$nfoFile\""; exec($command, $output, $return_var);
2. Capture Error Messages (See What's Actually Wrong)
By default, exec() only captures standard output (stdout). Any error messages from infekt-cmd.exe go to standard error (stderr), which is why your $output array is empty. Redirect stderr to stdout to see the issue:
$command = "\"$exePath\" -S --utf-16 \"$nfoFile\" 2>&1"; exec($command, $output, $return_var); // Print the output to debug print_r($output);
This will show you specific errors like "file not found" or invalid argument syntax.
3. Validate File and Executable Paths
- Check the executable path: Double-check that
infekt-cmd.exeexists at the specified location. Even Windows can be picky about case in some environments, so confirm the folder names match exactly (e.g.,iNFektvsinfekt). - Use absolute paths for the NFO file: If
$nfoFileis a relative path, PHP's working directory might not be what you expect. Userealpath()to verify the file exists and get its absolute path:$nfoFile = realpath($nfoFile); if (!$nfoFile) { die("Error: NFO file not found at specified path!"); }
4. Verify Permissions
- Ensure the user running PHP (e.g.,
IIS_IUSRSfor IIS,www-datafor Apache) has:- Execute permission on
infekt-cmd.exe - Read permission on your NFO file
- Execute permission on
- Check your
php.inifile to make sureexecisn't listed indisable_functions(this blocks PHP from running external commands).
5. Test the Command Manually First
Open Windows Command Prompt (CMD) and run the exact command you're trying to execute in PHP. For example:
"C:\Program Files\iNFekt\infekt\infekt-cmd.exe" -S --utf-16 "C:\full\path\to\your\file.nfo"
If this fails in CMD, fix the command there first—this eliminates PHP as the source of the problem.
内容的提问来源于stack exchange,提问作者Snake Eyes

