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
Check If the Current Date Is Between Two Dates + MySQL Select Query
PHP Upload File Enhance Security
PHP MySQL Group by to Get Latest Record, Not First Record
Converting Float Decimal to Fraction
Magento Products by Categories
How to Find "Related Items" in PHP
How to Skip Elements in Foreach Loop
Setting Up PHPmailer with Office365 Smtp
How to Implement Ws-Security 1.1 in PHP5
How to Disable the Back Browser Button After User Press Logout and Destroy Session
Extend Request Class in Laravel 5
How to Reference a Column Other Than 'Id' for a Joincolumn
Howto Generate JSON with Smarty
Phpmailer - Mails Going Straight to Spam
Prevent Back Button After Logout