@@ -147,7 +147,7 @@ public function getParts(): array
147147 public function getRealPath (): string
148148 {
149149 if (is_null ($ this ->realPath )) {
150- $ this ->realPath = str_replace ($ this ->getDirPath (), "" , $ this ->uri ->getPath ());
150+ $ this ->realPath = str_replace (rtrim ( $ this ->getDirPath (), " / " ), "" , $ this ->uri ->getPath ());
151151 }
152152 if (!is_string ($ this ->realPath )) {
153153 throw new \Exception ("Could not create realPath " , 1 );
@@ -158,8 +158,29 @@ public function getRealPath(): string
158158 /**
159159 * Extract and get directories from the simulated htaccess path
160160 * @return string
161- */
161+ */
162162 public function getDirPath (): string
163+ {
164+ if (is_null ($ this ->dirPath )) {
165+ // Absolute path to the "httpdocs" directory (document root)
166+ $ documentRoot = (isset ($ _SERVER ['DOCUMENT_ROOT ' ])) ? $ _SERVER ['DOCUMENT_ROOT ' ] : "" ;
167+ $ documentRoot = htmlspecialchars ($ documentRoot , ENT_QUOTES , 'UTF-8 ' );
168+
169+ // Absolute path to the current script's directory
170+ $ currentScriptDir = $ this ->request ->getUri ()->getDir ();
171+
172+ // Calculate the relative path from "httpdocs" to the script
173+ $ relativePath = str_replace ($ documentRoot , '' , $ currentScriptDir );
174+
175+ // Trim any leading or trailing slashes from the relative path
176+ $ this ->dirPath = "/ " . trim ($ relativePath , '/ ' );
177+ }
178+
179+ return $ this ->dirPath ;
180+ }
181+
182+ /*
183+ public function getDirPathOLD(): string
163184 {
164185
165186 if (is_null($this->dirPath)) {
@@ -178,7 +199,8 @@ public function getDirPath(): string
178199 }
179200 return $this->dirPath;
180201 }
181-
202+ */
203+
182204 /**
183205 * Get expected slug from path
184206 * @return string
@@ -284,6 +306,7 @@ public function getRoot(string $path = "", bool $endSlash = false): string
284306 if (!is_null ($ this ->handler )) {
285307 $ url .= $ this ->handler ->getPublicDirPath ();
286308 }
309+ $ url = rtrim ($ url , '/ ' );
287310 return $ url . (($ endSlash ) ? "/ " : "" ) . $ path ;
288311 }
289312
0 commit comments