Hướng dẫn php chdir not working

I'm developing a PHP cron script to check if a server is up or not (Ping).

Here is my code :

// Remonte d'un dossier
chdir('../');

// Inclusion du header pour avoir les infos de connexion à la db, fonctions, etc ...
require_once('./includes/header.php');

// Requête pour récupérer toutes les IP à pinger (Non-exclues donc)
$sReqGetAllServers = "  SELECT
                            *
                        FROM
                            host
                        WHERE
                            exclude_machine = :exclude_machine";

// Préparation de la requête
$oDatabase->Prepare($sReqGetAllServers);

// On bind la valeur au paramètre dans le WHERE
$oDatabase->BindValue(':exclude_machine', 'n', PDO::PARAM_STR);

// Exécution de la requête
$oDatabase->Execute();

// On associe les données dans un tableau à deux dimensions associatif
$aServers = $oDatabase->Assoc();

So at the first line, I need to make a chdir('../'); to include the necessary files (header.php).

I tested the script with Chrome and all worked fine but when I execute the script with command-line, PHP drop this error :

[14:40]  / >> php -f /web/dev/company/public_html/dasPing/cron/cron.php 
PHP Warning:  require_once(./includes/header.php): failed to open stream: No such file or directory in /var/www/html/dev/diadeis/public_html/dasPing/cron/cron.php on line 7

Warning: require_once(./includes/header.php): failed to open stream: No such file or directory in /var/www/html/dev/diadeis/public_html/dasPing/cron/cron.php on line 7
PHP Fatal error:  require_once(): Failed opening required './includes/header.php' (include_path='/usr/share/php') in /var/www/html/dev/diadeis/public_html/dasPing/cron/cron.php on line 7

Fatal error: require_once(): Failed opening required './includes/header.php' (include_path='/usr/share/php') in /var/www/html/dev/diadeis/public_html/dasPing/cron/cron.php on line 7

I searched why PHP drop this but I can't find any answer.

Do anyone knows why this is happening ?

Thank you !!

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

Nội dung chính

  • Description
  • Return Values
  • Errors/Exceptions
  • How do I move up a directory in PHP?
  • How do I view a directory in PHP?
  • What is chdir in PHP?
  • What is __ DIR __ in PHP?

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 move up a directory in PHP?

If you are using PHP 7.0 and above then the best way to navigate from your current directory or file path is to use dirname(). This function will return the parent directory of whatever path we pass to it.

How do I view a directory in PHP?

PHP scandir() Function $b = scandir($dir,1);

What is chdir in PHP?

The chdir() function changes the current directory.

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.