PHP - Convert File system path to URL

后端 未结 11 1500
野的像风
野的像风 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:36

    I've used this and worked with me:

    $file_path=str_replace('\\','/',__file__);
    $file_path=str_replace($_SERVER['DOCUMENT_ROOT'],'',$file_path);
    $path='http://'.$_SERVER['HTTP_HOST'].'/'.$file_path;
    

    And if you need the directory name in url format add this line:

    define('URL_DIR',dirname($path));
    

提交回复
热议问题