Akash
Akash

Reputation: 1726

Moving up a directory

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

Upvotes: 4

Views: 11229

Answers (4)

initall
initall

Reputation: 2385

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

Upvotes: 4

Teneff
Teneff

Reputation: 32158

you can create a directory on the upper level like this

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

without move in it

Upvotes: -1

Steve Mayne
Steve Mayne

Reputation: 22818

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.

Upvotes: 1

yan
yan

Reputation: 20982

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.

Upvotes: 2

Related Questions