PHP - Convert File system path to URL

后端 未结 11 1497
野的像风
野的像风 2020-11-29 08:12

I often find that I have files in my projects that need to be accessed from the file system as well as the users browser. One example is uploading photos. I need access to t

11条回答
  •  再見小時候
    2020-11-29 08:58

    Assume the directory is /path/to/root/document_root/user/file and the address is site.com/user/file

    The first function I am showing will get the current file's name relative to the World Wide Web Address.

    $path = $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];
    

    and would result in:

    site.com/user/file
    

    The second function strips the given path of the document root.

    $path = str_replace($_SERVER['DOCUMENT_ROOT'], '', $path)
    

    Given I passed in /path/to/root/document_root/user/file, I would get

    /user/file
    

提交回复
热议问题