Php change directory to root

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)

Php change directory to root

hakre

187k48 gold badges419 silver badges802 bronze badges

asked Jul 18, 2011 at 10:01

5

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']);

answered Jul 18, 2011 at 10:15

Ondrej SlintákOndrej Slinták

30.7k20 gold badges92 silver badges125 bronze badges

  • 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)

answered Jul 18, 2011 at 11:25

sdolgysdolgy

6,8573 gold badges40 silver badges60 bronze badges

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

answered Jul 18, 2011 at 10:05

genesisgenesis

49.8k20 gold badges94 silver badges123 bronze badges

4

(PHP 4 >= 4.0.5, PHP 5, PHP 7, PHP 8)

chrootChange the root directory

Description

chroot(string $directory): bool

This function is only available to GNU and BSD systems, and only when using the CLI, CGI or Embed SAPI. Also, this function requires root privileges.

Calling this function does not change the values of the __DIR__ and __FILE__ magic constants.

Parameters

directory

The path to change the root directory to.

Return Values

Returns true on success or false on failure.

Examples

Example #1 chroot() example

chroot("/path/to/your/chroot/");
echo 
getcwd();
?>

The above example will output:

Notes

Note: This function is not implemented on Windows platforms.

Note: This function is not available in PHP interpreters built with ZTS (Zend Thread Safety) enabled. To check whether your copy of PHP was built with ZTS enabled, use php -i or test the built-in constant PHP_ZTS.

(PHP 4, PHP 5, PHP 7, PHP 8)

chdirChange directory

Description

chdir(string $directory): bool

Parameters

directory

The new current directory

Return Values

Returns true on success or false on failure.

Errors/Exceptions

Throws an error of level E_WARNING on failure.

Examples

Example #1 chdir() example

// current directory
echo getcwd() . "\n";chdir('public_html');// current directory
echo getcwd() . "\n";?>

The above example will output something similar to:

/home/vincent
/home/vincent/public_html

Notes

Caution

If the PHP interpreter has been built with ZTS (Zend Thread Safety) enabled, any changes to the current directory made through chdir() will be invisible to the operating system. All built-in PHP functions will still respect the change in current directory; but external library functions called using FFI will not. You can tell whether your copy of PHP was built with ZTS enabled using php -i or the built-in constant PHP_ZTS.

See Also

  • getcwd() - Gets the current working directory

nesk at xakep dot ru

1 year ago

When working with FFI under a PHP ZTS environment, there is no standard way to change the directory with libraries (dll/so/dylib/etc), so to get around this problem, you should use something like this polyfill:

$directory

= 'path/to/libraries';

switch (\

PHP_OS_FAMILY) {
    case
'Windows':
        \
FFI::cdef('extern unsigned char SetDllDirectoryA(const char* lpPathName);', 'kernel32.dll')
            ->
SetDllDirectoryA($directory)
        ;
        break;

    case

'Linux':
    case
'BSD':
        \
FFI::cdef('int setenv(const char *name, const char *value, int overwrite);')
            ->
setenv('LD_LIBRARY_PATH', $directory, 1)
        ;
        break;

    case

'Darwin':
        \
FFI::cdef('int setenv(const char *name, const char *value, int overwrite);')
            ->
setenv('DYLD_LIBRARY_PATH', $directory, 1)
        ;
        break;
}
?>

php dot duke at qik dot nl

13 years ago

When changing dir's under windows environments:

$path="c:\temp"';
chdir($path);
/* getcwd() gives you back "c:\temp" */

$path="c:\temp\"'

;
chdir($path);
/* getcwd() gives you back "c:\temp\" */
?>

to work around this inconsistency
doing a chdir('.') after the chdir always gives back "c:\temp"

herwin at snt dot utwente dot nl

16 years ago

When using PHP safe mode and trying to change to a dir that is not accessible due to the safe mode restrictions, the function simply fails without generating any kind of error message.

(Tested in PHP 4.3.10-16, Debian Sarge default)

How do I change directories in PHP?

The chdir() function changes the current directory.

How do I get to the root directory?

To locate the system root directory: Press and hold the Windows key, then press the letter 'R'. (On Windows 7, you can also click start->run… to get the same dialog box.) Enter the word “cmd” in the program prompt, as shown, and press OK.

What is __ DIR __ in PHP?

The __DIR__ can be used to obtain the current code working directory. It has been introduced in PHP beginning from version 5.3. It is similar to using dirname(__FILE__). Usually, it is used to include other files that is present in an included file.

What is chroot PHP?

The chroot() function in PHP is an inbuilt function which is used to change the root directory of the current process to directory. The chroot() function changes the current working directory to “/”.