Php: How to Set Current Working Directory to Be Same as Directory Executing the Script

Executing a called php script as if it was in the same working directory as the calling script

Have you tried adding to the include path where PHP searches for includes?

$path = '/www/code/laboratory/';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);

Read more Documentation on that here

In PHP, Best way to ensure current working directory is same as script , when using CLI

chdir(dirname(__FILE__));

PHP exec() command: how to specify working directory?

Either you change to that directory within the exec command (exec("cd Scripts && ./script.sh")) or you change the working directory of the PHP process using chdir().

PHP create file in same directory as script

Try using __DIR__ . "/myText.txt" as filename.

http://php.net/manual/en/language.constants.predefined.php

how to use chdir to change to current directory?

First you need to store the current path, before changing dirs:

$oldPath = getcwd();
chdir('/some/path');
include(./file.php);
chdir($oldPath);

Why do you need to change dirs in order to include a file?

Does PHP include_path use working directory as fallback?

This is expected behavior. From the include() documentation:

Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing. The include construct will emit an E_WARNING if it cannot find a file; this is different behavior from require, which will emit an E_ERROR.

And as noted, include and require are identical in how they look up files, and only differ in how they react if a file is not found.



Related Topics



Leave a reply



Submit