4

I'm creating a directory in a php script, moving into that directory and doing some stuff. This part works fine.

Now, I want to move out of this directory and delete it. Any pointers on how to do this?

I have tried moving up 1 level, but it doesnt work

chdir("../");
chdir("..")
system("cd ..");

none of the above have any effect on the present working directory

tshepang
  • 12,111
  • 21
  • 91
  • 136
Akash
  • 1,716
  • 2
  • 23
  • 43

4 Answers4

4

You could do something like this to be more explicit about your intended paths:

$cwd = getcwd(); // remember the current path
chdir(<your target path>);
chdir($cwd); // go back to the inital working directory
initall
  • 2,385
  • 19
  • 27
2

Don't chdir into that directory once you create it, modify files from outside it, and then remove it.

i.e. instead of doing something like:

mkdir("dir");
chdir("dir");
write_file("one");
write_file("two");

structure your code as:

mkdir("dir");
write_file("dir/one");
write_file("dir/two");

You then don't need to walk the directory hierarchy.

yan
  • 20,644
  • 3
  • 38
  • 48
1

I would advise against actually changing dir; simply hold the path of interest and operate upon it (and the files within it) directly.

That said, chdir ('../'); should work.

Steve Mayne
  • 22,285
  • 4
  • 49
  • 49
-1

you can create a directory on the upper level like this

<?
   mkdir('../newdir/', 0755);
?>

without move in it

Teneff
  • 30,564
  • 13
  • 72
  • 103