php chdir() direct to home directory
How do i change the directory in php : I tried this :
chdir('\');
But this doesnt changes the directory. I am us开发者_运维知识库ing php-cli
I will prefer a cross platform solution(i.e. common for windows , linux , mac)
If you want to change to home directory, you can use:
chdir($_SERVER['HOMEPATH']);
It's even cross-platform this way.
EDIT:
If you mean "home directory" as top directory on drive, you can use:
chdir($_SERVER['HOMEDRIVE']);
- http://alanhogan.com/tips/php/directory-separator-not-necessary
As long as you use the forward slash, “/”, you’ll be OK. Windows doesn’t mind it, and it’s best for *nix operating systems.
Based on the above:
chdir('/');
// current directory
echo getcwd() . "\n";
Do you see errors anywhere?
Warning: chdir() [function.chdir]: Permission denied (errno 13)
if (stristr (PHP_OS, 'Win')) {
chdir('C:\\');
} elseif (stristr (PHP_OS, 'Lin')) {
chdir('/');
} else {
chdir($_SERVER['HOMEPATH']);
//i am not sure what does MAC use as its root
}
try this one. By single \ you're escaping single quote
精彩评论