从Php脚本执行.exe文件时遇到的错误

时间:2012-11-16 05:08:07

标签: php exe

  

可能重复:
  Why is PHP not replacing the variable in string?

我一直在尝试从Php执行此行echo exec('hi.exe $file',$results,$status);。分配给$ file的值是文件名 hi.txt (即$ file = hi.txt)。

但每次我尝试使用相同的行运行我的代码时,它显示的错误为$file file not found,就好像我在命令提示符中运行相同的hi.exe hi.txt一样。

并且如果我尝试使用文件名而不是来自php的变量运行相同的行,即exec('hi.exe hi.txt',$results,$status),浏览器会长时间执行而不提供输出。

请有人告诉我哪里出错了!

2 个答案:

答案 0 :(得分:2)

您使用的是单引号,而不是双引号。将echo exec('hi.exe $file',$results,$status);更改为:

echo exec("hi.exe $file",$results,$status);

或使用点,如下所示:

echo exec('hi.exe '.$file,$results,$status);

在PHP中,使用单引号不会将$file变为hi.txt;它只是作为文字字符串“$ file”保留。使用双引号或点连接实际将$file扩展为hi.txt

答案 1 :(得分:1)

单引号不会扩展变量。你可能意味着:

echo exec("hi.exe $file",$results,$status);