kritya
kritya

Reputation: 3362

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 using php-cli

I will prefer a cross platform solution(i.e. common for windows , linux , mac)

Upvotes: 0

Views: 7601

Answers (3)

sdolgy
sdolgy

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

Ondrej Slinták
Ondrej Slinták

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

genesis
genesis

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

Related Questions