Running shell commands in a .php file from the command line

I have a series of shell commands that I want to put into the program and execute the program from the command line. I decided to use PHP for this, so I'm currently trying to run most shell commands.

Save as build.php

<?php
shell_exec('cd ..');
echo "php executed\n";
?>

From the command line

php build.php

Exit

php executed

Php is executing correctly, but I'm still in the same directory. How to get shell_exec (...) to successfully invoke a shell command?

0
source share
1 answer

cwd ( ) PHP... cd, exec(), , exec() , .

<?php
    $olddir = getcwd();
    chdir('/path/to/new/dir');  //change to new dir
    exec('somecommand');

somecommand /path/to/new/dir.

<?php
    exec('cd /path/to/new/dir');
    exec('somecommand');

somecommand , PHP script from - the cd, , .

, - :

<?php
    exec('cd /path/to/new/dir ; somecommand');

... , , .

+4

Source: https://habr.com/ru/post/1614929/


All Articles