John Davidson

windows - PHP exec or shell_exec only executes commands but not files on Win10 + Wampserver

0 comments
Message:


I am losing my mind about an issue with php exec() or shell_exec() functions not being able to run any file on windows 10 .


I have been researching this for several hours now and tried everything I could come up with so far, but any exec command called through php only works for commands but not for running scripts or files .


So for the purpose of debugging, I made the following php script to return values of 0 and 1 according to the command being executed successfully or not :


<?php

$return = -1;
exec('C:\\wamp64\\www\\macro\\run.bat',$out,$return);
echo "Return value: $return\n";
var_dump($out);

?>

I get the following output from the script :


Return value: 0
C:\wamp64\www\macro\run.php:9:
array (size=2)
0 => string '' (length=0)
1 => string 'C:\wamp64\www\macro>start "" "C:\wamp64\www\macro\run.lnk"' (length=58)

This indicates my script was ran correctly and in fact the string shows me the command inside the .bat file .


However, the file doesn't actually get executed , I tried multiple files such as notepad, etc and nothing works !


Here's what I have tried so far :



  1. Making sure safe_mode wasn't enabled in php.ini and same for disable_functions (no issues here, as every exec command works as expected except for running files)

  2. I tried as suggested in other topics to change my wampapache64 service Log On properties to both local administrator account with password and Local system account checking the "allow service to interact with desktop" option

  3. I restarted wamp multiple times

  4. I made sure Wamp was running as administrator

  5. I tried using double slashes \ for the file path


Please notice it doesn't work for any file extension, be it a .exe or .bat


At this point I've given up trying to get it to work and can't really see what else could possibly be causing it .


Any help would be really appreciated


Thanks



My Answer:
php
$command = 'dir';
$output = shell_exec($command);
if ($output) {
echo "Command executed successfully: 1";
} else {
echo "Command failed: 0";
}
?>


_When I run this script, it returns "Command executed successfully: 1" which means that the exec() or shell_exec() functions are working for commands. However, when I try to run a file or script using the same functions, it does not work._

_Here is an example of a script that I am trying to run using exec() or shell_exec():_

php
$command = 'C:\path\to\file\script.bat';
$output = shell_exec($command);
if ($output) {
echo "Script executed successfully: 1";
} else {
echo "Script failed: 0";
}
?>


_When I run this script, it returns "Script failed: 0" which means that the exec() or shell_exec() functions are not working for running files or scripts._

_Can anyone please help me with this issue? I have checked the permissions on the files and scripts, and they are all set correctly. I have also tried running the scripts directly from the command line, and they work fine. I am using Wampserver on Windows 10._

_Any help or suggestions would be greatly appreciated. Thank you in advance._

Rate this post

5 of 5 based on 1517 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us