how to change the path dynamically according to? |
Post Reply | Page <123> |
Author | |
Dmit OE
Admin Group Joined: May 31 2012 Status: Offline Points: 5283 |
Post Options
Thanks(1)
|
P.S. If you your case the site is always located in a folder that's named in a certain way and it won't change, you can use that as well.
|
|
Dmit OE
Admin Group Joined: May 31 2012 Status: Offline Points: 5283 |
Post Options
Thanks(1)
|
Or, to not bother about all this, here is a universal solution, given that you do not have any index.php or index.htm in subfolders of your project somewhere. Insert this into a Code Block Source type PHP in your baselayer or anywhere else:
function findSiteRootPath() { $path = __DIR__; // page or script's absolute path, somewhere in the project // find the folder where index.php or index.htm is, starting from the current folder: while ((strpos($path, '/') || strpos($path, '\\')) && !file_exists("$path/index.php") && !file_exists("$path/index.htm")) { $path = dirname($path); } return $path; // } $root = findSiteRootPath(); // Test: echo "<br><br>Path to the Files/Other folder: $root/Files/Other"; |
|
djju1029
Senior Member Joined: Aug 02 2016 Location: the US Status: Offline Points: 126 |
Post Options
Thanks(0)
|
I need your help, a solution using certain Packs auxiliary functionality. I couldn't do a beacon method to find the physical root path of the website....
Many many thanks, DJ
|
|
Dmit OE
Admin Group Joined: May 31 2012 Status: Offline Points: 5283 |
Post Options
Thanks(1)
|
Check my latest pos, it should suit you
|
|
djju1029
Senior Member Joined: Aug 02 2016 Location: the US Status: Offline Points: 126 |
Post Options
Thanks(0)
|
DmitOE,
I found that $_SERVER["PHP_SELF"] returns same result from PC or my Raspberry Pi such as /test/manual_scripting (not \ from pc) Therefore, I wrote simple function that this path to relative dotted path as following: function changeCurrentPath2RelativeDot(){ $testpath = explode("/", dirname($_SERVER['PHP_SELF'])); $no_double_dot = count($testpath) - 1; $testpath = str_repeat('../', $no_double_dot); return $testpath; } $relative_dot = changeCurrentPath2RelativeDot(); // Test: echo "<br><br>RelativePath to the Files/Other folder: ",$relative_dot,"Files/Other"; //result is ../../Files/Other I checked the $relative_dot gives corrent number of dot path such as ../../../ where page is. I put this function into Start of Document of BaseLayer. But I got another problem. I need to use $relative_dot on CSS link in the header like this: <Absolute> <link href="C:/Users/djoo/Desktop/PortableGit/project/NMU_mockup/WEFiles/Other/jquery-ui.css" rel="stylesheet"> <Relative> <link href="../../WEFiles/Other/jquery-ui.css" rel="stylesheet"> when $relative_dot is ../../ How can I change this absolute to relative path in HTML using the result of my function?
Edited by djju1029 - Feb 03 2017 at 6:35pm |
|
Dmit OE
Admin Group Joined: May 31 2012 Status: Offline Points: 5283 |
Post Options
Thanks(0)
|
I don't understand where you are blocked; you seem to have found an effcient function to get the path "back" to the root. By the way it is relative to the page location, not an included script. Why not simply prepend it to "/WEFiles/Other/jquery-ui.css"?
|
|
djju1029
Senior Member Joined: Aug 02 2016 Location: the US Status: Offline Points: 126 |
Post Options
Thanks(0)
|
Where I am blocked is prepend my result of "$relative_dot" to link in the header.
This question comes from when I look your source. When I look into original source view of page, I found the link of header is relative dot to back as following: It is original source view of page and link of css file in header: <link id="openElement" rel="stylesheet" type="text/css" href="../../../WEFiles/Css/v02/openElement.css?v=50491101600" /> I just want to copy your way in my source, so I found how to change current absolute dirpath to relative ../ to get back to root. But I have no idea how to prepend it to link path commonly. If I put $relative_dot into the link, but it don't work.... When I look at source view of page, I found: var WEInfoPage = {"PHPVersion":"phpOK","OEVersion":"1-56-0","PagePath":"test/examples/test_folder/Copy-BlankLayer","Culture":"DEFAULT","LanguageCode":"EN-US","RelativePath":"../../../","RenderMode":"Export","PageAssociatePath":"test/examples/test_folder/Copy-BlankLayer","EditorTexts":null}; See, there is "RelativePath":"../../../" Eureka! I think it has some secret on your source that change path relatively to the root. I just want to figure out how you realize this way and to copy it on my source.....
|
|
Dmit OE
Admin Group Joined: May 31 2012 Status: Offline Points: 5283 |
Post Options
Thanks(1)
|
This is calculated inside OE, who knows where each page is exactly. There's a way to get it in PHP version if you use Packs on the page, but it is not necessary I think. Your code allows to get the same value (is it?), ex. "../../../", so you just do something like:
echo "<link href='{$relative_dot}Files/Other/jquery-ui.css' rel='stylesheet'>"; Why woud this not work? Just not WEFiles/Other but Files/Other (and place the file jquery-ui.css accordingly). I have a n impression that you are missing something rather obvious. If nothing works, send me your project via a private message with the code that does not work, I will look why when I have time. |
|
djju1029
Senior Member Joined: Aug 02 2016 Location: the US Status: Offline Points: 126 |
Post Options
Thanks(0)
|
Many many thanks to DmitOE. Finally, I did it like ths,
<include, by PHP at start of document of BaseLayer.php> function changeCurrentPath2RelativeDot(){ $testpath = explode("/", dirname($_SERVER['PHP_SELF'])); $no_double_dot = count($testpath) - 1; $testpath = str_repeat('../', $no_double_dot); return $testpath; } $relative_dot = changeCurrentPath2RelativeDot(); include_once($relative_dot."Templates/includes/nmudb.php"); require_once($relative_dot."Templates/includes/php_serial.class.php"); <jquery_ui, by HTML at head of BaseLayer.php> <?php echo "<link href='{$relative_dot}WEFiles/Other/jquery-ui.css' rel='stylesheet'>"; ?> <br> <?php echo "<script type='text/javascript' src='{$relative_dot}WEFiles/Other/jquery-ui.js'></script>"; ?> every page woks! Thank you, my mentor! Edited by djju1029 - Feb 03 2017 at 10:39pm |
|
djju1029
Senior Member Joined: Aug 02 2016 Location: the US Status: Offline Points: 126 |
Post Options
Thanks(0)
|
DmitOE, I found an error in my code.
As OE at PC local server, dirname($_SERVER['PHP_SELF']); is "\". Therefore, explode("/", dirname($_SERVER['PHP_SELF'])); is array(1) { [0]=> string(1) "\" } BUT, AS webserver at RPi, dirname($_SERVER['PHP_SELF']); is "/". Therefore, explode("/", dirname($_SERVER['PHP_SELF'])); is array(2) { [0]=> string(0) "" [1]=> string(0) "" } Consequently, my source never point to home root where PHP_SELF is home. Finally, I debug my code as following; function changeCurrentPath2RelativeDot(){ $current_path = dirname($_SERVER['PHP_SELF']); if ($current_path == "/") { $testpath = ""; return $testpath; } else { $testpath = explode("/", $current_path); $no_double_dot = count($testpath) - 1; $testpath = str_repeat('../', $no_double_dot); return $testpath; } } $relative_dot = changeCurrentPath2RelativeDot(); include_once "{$relative_dot}Templates/includes/nmudb.php"; require_once "{$relative_dot}Templates/includes/php_serial.class.php"; Sorry for making your confusion. Edited by djju1029 - Feb 07 2017 at 4:09pm |
|
Post Reply | Page <123> |
Tweet
|
Forum Jump | Forum Permissions You cannot post new topics in this forum You cannot reply to topics in this forum You cannot delete your posts in this forum You cannot edit your posts in this forum You cannot create polls in this forum You cannot vote in polls in this forum |