How does the opendir function interpret paths starting with a slash in PHP?

When using the opendir function in PHP, paths starting with a slash are interpreted as absolute paths, meaning they start from the root directory of the filesystem. To solve this issue and ensure the opendir function interprets paths correctly, you can check if the path starts with a slash and remove it before passing it to the function.

$path = "/path/to/directory";
if (strpos($path, "/") === 0) {
    $path = substr($path, 1);
}
$dir = opendir($path);