Jump to content

PHP Exec Function Doesn't work with cd Ubuntu Command


unemployment

Recommended Posts

When I run

 

$dir = shell_exec('cd /var/www/trekeffect.com/trekeffect.com;pwd');

$this->log($dir);
//chdir('/trekeffect.com/trekeffect.com');
//shell_exec('cd /var/www/trekeffect.com/trekeffect.com', $output);
         
$test = shell_exec('pwd');
$this->log($test);

It logs:

2013-04-12 14:36:36-04:00 --- INFO: /var/www/trekeffect.com/trekeffect.com

2013-04-12 14:36:36-04:00 --- INFO: /var/www/deploy

 

It seems to be going back to the first deploy directory. Do you know why?

Edited by unemployment
Link to comment
Share on other sites

Wow.....very strange directories names :)

 

You don't have to execute shell_exec() every time, just put your logic into a simple string and execute it at once.

 

Something like:

<?php

$str = 'cd /var/www/trekeffect.com/trekeffect.com;';

$str .='pwd;';

$str .= 'ls -d'; // etc.....

//execute the string

$dir = shell_exec($str); 

echo $dir; 

Don't forget to separate the individual unix commands with semicolon

Edited by jazzman1
Link to comment
Share on other sites

shell_exec executes code in a new shell process. Meaning, if you call cd, it only changes directory for that process. Any subsequent calls to shell_exec will be executed from the original directory.

 

I don't see why you would be using php for this at all. Use bash, and your issues will go away.

Link to comment
Share on other sites

shell_exec executes code in a new shell process. Meaning, if you call cd, it only changes directory for that process. Any subsequent calls to shell_exec will be executed from the original directory.

 

I don't see why you would be using php for this at all. Use bash, and your issues will go away.

 

Tony, I'm agree with you, in that case BASH is gonna be much more suitable and powerful than php.

Link to comment
Share on other sites

  • 10 months later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.