Reputation: 3362
How do i change the directory in php : I tried this :
chdir('\');
But this doesnt changes the directory. I am using php-cli
I will prefer a cross platform solution(i.e. common for windows , linux , mac)
Upvotes: 0
Views: 7601
Reputation: 7001
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)
Upvotes: 1
Reputation: 31930
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']);
Upvotes: 1
Reputation: 50974
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
Upvotes: 0