Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Base Path In PHP

Tags:

php

I need to change the folder that "relative include paths" are based on.

I might currently be "in" this folder: C:\ABC\XYZ\123\ZZZ

And in this case, the path "../../Source/SomeCode.php" would actually be in this folder: C:\ABC\XYZ\Source

And realpath('.') would = 'C:\ABC\XYZ\123\ZZZ';

If however, realpath('.') were "C:\Some\Other\Folder"

Then in this case, the path "../../Source/SomeCode.php" would actually be in this folder: C:\Some\Source

How do I change what folder is represented by '.' in realpath()?

Like this:

echo ('BEFORE = '.realpath('.')); // BEFORE = C:\ABC\XYZ\123\ZZZ
// Some PHP code here...
echo ('AFTER = '.realpath('.')); // AFTER = C:\Some\Other\Folder

How can I change the folder represented by '.', as seen by realpath()?

like image 562
Joshua Avatar asked Sep 28 '26 18:09

Joshua


2 Answers

The function chdir() does this. For example:

echo ('BEFORE = '.realpath('.')); // BEFORE = C:\ABC\XYZ\123\ZZZ
chdir('C:/Some/Other/Folder');
echo ('AFTER = '.realpath('.')); // AFTER = C:\Some\Other\Folder
like image 72
Arda Xi Avatar answered Oct 01 '26 08:10

Arda Xi


Use the chdir() function.

like image 21
wallyk Avatar answered Oct 01 '26 07:10

wallyk