shell_exec not executing code - returns 'cmd'

Links for php scripts

Moderators: egami, macek, gesf

Post Reply
pizzipie
php-forum Active User
php-forum Active User
Posts: 30
Joined: Sun Aug 04, 2019 4:10 pm

Mon Nov 23, 2020 10:47 am

Trying to get shell_exec() to work inside Firefox.
There is something I'm missing or don't just get!!

Any help in fixing this is greatly appreciated!!

Process:

# I am using AJAX to call xxx.php(pass 1) which creates a select box.
# Select file and use AJAX to call xxx.php(pass 2)
# If file has extension ".sql.gz" use the shell_exec() function to unzip file
# When file is unzipped use the shell_exec() function to restore the MySql database file.

Frontend code:

Code: Select all

 $(document).ready(function(){
        $('#selbox').on('change',function(){
            var choice= $("#selbox option:selected").text();
            var verify=confirm("Restore - "+choice+"?");
            if (verify) {
                $.ajax({
                    type: "POST",
                    url: "pizselectdb2.php",
                    data: {"pass":2, "file":choice},
                    dataType: "text"
                })               
                
                .done(function (result) {
                    alert(result);
	                location.href="PizziFrontEnd.php";
                });                                             
            }    // verify
            else {
                location.href="PizziFrontEnd.php"
            }

        }); // change
    });  //ready
xxx.php code:

Code: Select all

elseif($_POST['pass']==2) {
    
    $ext = strstr($filetosav, ".");
  
    if($ext == ".sql.gz") {
        $zip="unzip";
    }    
    elseif ($ext == "sql") {
        $zip="no-unzip";
    }
    
     if($zip == "unzip") {
         $cmd="gunzip ".$dir."/".$filetosav;
         $x=shell_exec($cmd);
     }


Problem: shell_exec() function will not execute the command!

File directory ownership is www-data:rick as are files within.
There are no /var/log/apache2 error.log entries for this.


Output of Alert(result):
shellProblem.png
shellProblem.png (20.05 KiB) Viewed 2113 times
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 1222
Joined: Mon Feb 22, 2016 5:52 pm

Mon Nov 23, 2020 1:16 pm

Is there any reason for not using the built in functions?

Do you really need to use jQuery for this?
pizzipie
php-forum Active User
php-forum Active User
Posts: 30
Joined: Sun Aug 04, 2019 4:10 pm

Mon Nov 23, 2020 5:10 pm

Thanks for the reply.
hyper wrote:
Mon Nov 23, 2020 1:16 pm
Is there any reason for not using the built in functions?

Do you really need to use jQuery for this?
I need the html doc to show a select box and AJAX to get to the server to get the file and unzip it and update the MySql database. I do not know anything about PHP Zip stuff shown in the manual.

I'm assuming the PHP zip stuff coding will do all of this?????

HOWEVER, my question is still why doesn't exec() or shell_exec() work here?? I have a companion script that backs up these files using exec() and it works fine.
simonbrahan
php-forum Fan User
php-forum Fan User
Posts: 137
Joined: Mon Jun 08, 2020 2:00 am
Contact:

Tue Nov 24, 2020 7:03 am

Can you post the entire xxx.php file, so we can see what's going on?
pizzipie
php-forum Active User
php-forum Active User
Posts: 30
Joined: Sun Aug 04, 2019 4:10 pm

Wed Nov 25, 2020 6:51 pm

Thanks to help from users I discovered PHP gzip functions which work just fine.

However, I tried out:

Code: Select all

    $cmd="mysql -uxxxx -pxxxx < ".$filetosav;
    
    exec($cmd);
It works just fine.
Post Reply