Moving up a directory
I'm creating a directory in a php script, 开发者_JAVA百科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
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
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.
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.
you can create a directory on the upper level like this
<?
mkdir('../newdir/', 0755);
?>
without move in it
精彩评论