Thursday 26 March 2020

relative path, php getcwd


# one level up current dir
cd ..
# equivalent 
cd ./..
# This means .. is used after directory
# one levelup of /home/test/dir and go to /home/test/vendor
cd /home/test/dir/../vendor


#PHP working directory is the directory where you run the script
echo getcwd();

# If you run the script /home/test/myscript.php, working directory is /home/test
# If you have a file in /home/test/function/test.php, and you have another file in /home/test/vendor/myscript.php

# in /home/test/function/test.php, require_once '../vendor/myscript.php' will not work, because curretn working directory is /home/test. One level up that is /home, and there is no such thing as /home/vendor 


#solution
require_once __DIR__ . '/../vendor/myscript'

__DIR__ is current directory of file so /home/test/function, so /../ means one level up that means /home/test, then /home/test/vendor






No comments:

Post a Comment