luhfluh
luhfluh

Reputation: 484

PHP shell exec not working in HTML browser but working on cmd prompt

I am running PHP 5.2.14 on Apache/2.2.16 (Win32) and I have a script to run a shell command, which when tested in the command prompt works well but in browser mode (html), it doesn't.

Scripts:

mybatfile.bat

REM ...
REM process some folder details
REM code which does not work in browser mode but works in cmd mode
C:/somfolder/bin/mysqldump -u abc -pabcdef --result-file="C:/Apache22/somfolder/DBbackup/DBbackup.%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%.sql" --dump-date  --log-error="C:/Apache22/somfolder/DBbackup/DBbackup.%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%.log" dbname > dboutputresult.txt

REM process result of savedfile

calltobatfile.php

//process some folder information
$file = file_get_contents($folderroot."/exec/mybatfile.bat");
//var_dump($file);
    $strarr = explode("\n", $file);
    foreach($strarr as $line){
      if(strlen($line)>1){
        var_dump("line: ".$line."\n"); 
        $output = shell_exec($line);
        print_r($output);
     }
   }

//process result of savedfile additional information

What can I try next?

Upvotes: 0

Views: 2181

Answers (3)

cijagani
cijagani

Reputation: 523

here is a my code and its working fine.

$result = mysqli_query($mysqli_link,'SHOW VARIABLES LIKE \'%basedir%\';');
    $row = mysqli_fetch_array($result);
    $pathtodump = "".$row['Value']."/bin/mysqldump";    
    $command= "$pathtodump -h " . $db_host . " -u " . $db_user . " -p".$db_pass. " " . $db_dbname . "> filename.sql";
    exec($command);

Note try to give mysqldump.exe file name with full path

Upvotes: 2

Jacek Sokolowski
Jacek Sokolowski

Reputation: 597

Check the permissions to run it as a server. You can run this as owner, but there can be no possibilities to run it as other user - in your example as apache.

Upvotes: 3

Josejulio
Josejulio

Reputation: 1295

Maybe is a permission related issue? In your command line you may be using other User rather than the web one which may not have the required permissions to execute or write in C:/Apache22/somfolder/DBbackup/ or dboutputresult.txt

Upvotes: 1

Related Questions