PHP: Get absolute path from absolute URL

前端 未结 4 1638
闹比i
闹比i 2020-12-10 06:43

I have an absolute path of a file, Is there a way to get the file absolute path

http://domainname/rootfolder/filename.php

and I wanna get s

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-10 07:22

    This is the easiest way:

    $path = parse_url('http://domainname/rootfolder/filename.php', PHP_URL_PATH);
    
    //To get the dir, use: dirname($path)
    
    echo $_SERVER['DOCUMENT_ROOT'] . $path;
    

    That'll print you the full path.

    Documentation:

    parse_url

    DOCUMENT_ROOT

    dirname

提交回复
热议问题