Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>I got it!</strong> I only had to build a Rube Goldberg Machine to do it!</p> <p><em>Thanks PHP</em>.</p> <pre><code>$linkroot = ascertainLinkroot(); function ascertainLinkroot(){ return makeRelativePath( getTopScriptPath(), __DIR__ ); } function getTopScriptPath(){ $backtrace = debug_backtrace( defined( "DEBUG_BACKTRACE_IGNORE_ARGS") ?DEBUG_BACKTRACE_IGNORE_ARGS :FALSE ); $top_frame = array_pop($backtrace); $top_script_path = $top_frame['file']; return $top_script_path; } function makeRelativePath($from,$to){ // Compatibility $from = is_dir($from) ?rtrim($from,'\/').'/' :$from; $to = is_dir($to) ?rtrim($to,'\/').'/' :$to; $from = str_replace('\\','/',$from); $to = str_replace('\\','/',$to); //---------------------------- $from = explode('/',$from); $to = explode('/',$to); $path = $to; foreach($from as $depth =&gt; $dir) { if ($dir === $to[$depth]) { // find first non-matching dir array_shift($path); // ignore it } else { $remaining = count($from)-$depth; // get number of remaining dirs to $from if ($remaining&gt;1){ // add traversals up to first matching dir $padLength = -(count($path)+$remaining-1); $path = array_pad($path, $padLength, '..'); break; } else { $path[0] = './'.$path[0]; } } } return rtrim(implode('/', $path),'\/'); } </code></pre> <p>So, basically, I use the <code>makeRelativePath</code> function to calculate a relative path from the top script's absolute path to the current script's absolute directory path (<code>__DIR__</code>).</p> <p>I realized that I'm actually looking for the relative path to the library from the <em>top script</em>, not just the parent script -- because the top script is the one where clientside assets will need to be referenced in relation to.</p> <p>Naturally, PHP doesn't just <em>give</em> you the top script's absolute path. On some environments, the top script's path can be available as a $_SERVER variable, however environment independence is important for me, so I had to find a way.</p> <p><code>getTopScriptPath</code> was my solution, as it uses <code>debug_backtrace</code> to find it (creepy, I know), but it is the only environment-independent way to fetch it (to my knowledge).</p> <p><strong>Still hoping for a more elegant solution,</strong> but am satisfied that this one works.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload