Manchmal benötigt man Pfade der aktuellen Datei. Absolut, relativ, auch von inkludierten Dateien.
Dieser Codeschnippsel ist sehr hilfreich:
$pathVars['baseDir'] = __DIR__; $pathVars['baseDir'] = __DIR__; $pathVars['docRoot'] = filter_input(INPUT_SERVER, 'DOCUMENT_ROOT'); $pathVars['baseUrl'] = preg_replace('!^' . $pathVars['docRoot'] . '!', '', __DIR__); $pathVars['protocol'] = empty(filter_input(INPUT_SERVER, 'HTTPS')) ? 'http' : 'https'; $pathVars['file'] = __FILE__; $pathVars['fileUrl'] = preg_replace('!^' . $pathVars['docRoot'] . '!', '', __FILE__); $pathVars['fileName'] = preg_replace('!^' . __DIR__ . '/!', '', __FILE__); $pathVars['port'] = filter_input(INPUT_SERVER, 'SERVER_PORT'); $pathVars['uri'] = filter_input(INPUT_SERVER, 'REQUEST_URI'); $pathVars['viewedPort'] = ''; if ($pathVars['protocol'] == 'http' && $pathVars['port'] != 80 || $pathVars['protocol'] == 'https' && $pathVars['port'] != 443) { $pathVars['viewedPort'] = ':' . $pathVars['port']; } $pathVars['domain'] = filter_input(INPUT_SERVER, 'SERVER_NAME'); $pathVars['url'] = $pathVars['protocol'] . '://' . $pathVars['domain'] . filter_input(INPUT_SERVER, 'REQUEST_URI'); $pathVars['fullUrl'] = $pathVars['protocol'] . '://' . $pathVars['domain'] . $pathVars['viewedPort'] . $pathVars['baseUrl']; echo '<pre>'; print_r($pathVars); echo '</pre>';
Ausgabebeispiel:
Array ( [baseDir] => /www/htdocs/username/www/tipps.4uweb.de/dev/pathVars [docRoot] => /www/htdocs/username/www/tipps.4uweb.de/ [baseUrl] => dev/pathVars [protocol] => https [file] => /www/htdocs/username/www/tipps.4uweb.de/dev/pathVars/index.php [fileUrl] => dev/pathVars/index.php [fileName] => index.php [port] => 443 [port] => /dev/pathVars [viewedPort] => [domain] => tips.4uweb.de [url] => https://tips.4uweb.de/dev/pathVars/index.php [fullUrl] => https://tips.4uweb.dedev/pathVars )