-2

I am planning to execute an operation using the backtick operator in a foreach loop. I want to change to the desired directory before the loop, and then iterate without needing to change the directory back.

The following simple test seems to show that you can not stay in the same directory between operations:

$output=`pwd`."<br />";
$output.=`cd ~/domains && pwd`."<br />";
$output.=`pwd`;
echo $output;
die;

// output

home-path
home-path/domains
home-path

Is there a way to accomplish this? Or should I just cd to the directory every time I execute in the foreach loop?

dlolsen
  • 132
  • 1
  • 6

1 Answers1

0

chdir was the winner! Modified my test and it works fine:

$output=getcwd()."<br />";
chdir('home-path/domains');
$output.=getcwd()."<br />";
$output.=`pwd`;
echo $output;
die;

// output

home-path
home-path/domains
home-path/domains
dlolsen
  • 132
  • 1
  • 6