I'm using shell_exec in PHP which works if I use the executable in the same folder, but doesn't work anymore if I point executable in subfolder.

I have this (which works):

shell_exec ("makescreen.exe /url=".$link."");

I would like to have this (doesn't work - it just skips the execution):

shell_exec ("/screens/makescreen.exe /url=".$link."");

Oh, and paths are in Windows mode.

Thanks for your help guys.

有帮助吗?

解决方案 2

Be care of the difference of absolute path. relative path. execute path. For example, your dir structure is:

/home/my/php/script/test.php 
                   /makescreen.exe

and in you php script you call shell_exec(makescreen.exe ***).

In this case, if you execute your script like cd /home/my/php/script && php test.php, then the execute path is /home/my/php/script/ and the scirpt will find makescreen.exe in execute path, here is /home/my/php/script/

However, if you currently stay in /home/my and use this way php /home/my/php/script/test.php then the execute path is your current path, here is /home/my, and the script will find the makescreen.exe in /home/my, definitely failed.

If you use /home/my/php/script/makescreen.exe in your script, this is absolute path and wherever you are, it will find makescreen.exe in /home/my/php/script/

And if you want to put the executable file in subfolder. you can use relative path subfolder/makescreen.exe in your script and make sure the script can access it correctly.

其他提示

try using exec () with the complete path rather than relative path

 $oldcwd = getcwd();
 chdir($oldcwd+"/screens");
 exec("makescreen.exe /url=".$link."");
  chdir($oldcwd); 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top