PHP - Convert File system path to URL

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

    IMHO such automation is really error prone. You're far better off using some explicit path helpers (eg. one for uploads, one for user pics, etc) or just encapsulate for example an uploaded file with a class.

    // Some "pseudo code"
    $file = UploadedFile::copy($_FILES['foo']);
    $file->getPath(); // /var/www/example.org/uploads/foo.ext
    $file->getUri();  // http://example.org/uploads/foo.ext
    
    0 讨论(0)
  • 2020-11-29 08:55

    Try this:

    $imgUrl = str_replace($_SERVER['DOCUMENT_ROOT'], '', $imgPath)
    
    0 讨论(0)
  • 2020-11-29 08:56

    This simple snippet can convert the file path to file's url on the server. Some settings like protocol and port should be kept.

            $filePath = str_replace('\\','/',$filePath);
            $ssl = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? true : false;
            $sp = strtolower($_SERVER['SERVER_PROTOCOL']);
            $protocol = substr($sp, 0, strpos($sp, '/')) . (($ssl) ? 's' : '');
            $port = $_SERVER['SERVER_PORT'];
            $stringPort = ((!$ssl && $port == '80') || ($ssl && $port == '443')) ? '' : ':' . $port;
            $host = isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
            $fileUrl = str_replace($_SERVER['DOCUMENT_ROOT'] ,$protocol . '://' . $host . $stringPort, $filePath);
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-29 08:59

    I always use symlinks in my local development environment and @George's approach fails in this case:

    The DOCUMENT_ROOT is set to /Library/WebServer/Documents and there is a symlink /Library/WebServer/Documents/repo1 -> /Users/me/dev/web/repo1

    Assume that following codes are in /Users/me/dev/web/repo1/example.php

    $_SERVER['DOCUMENT_ROOT'] == "/Library/WebServer/Documents" //default on OS X
    

    while

    realpath('./some/relative.file') == "/Users/me/dev/web/repo1/some/relative.file"
    

    Thus, replacing DOCUMENT_ROOT with HTTP_HOST doesn't work.

    I come up with this little trick:

    function path2url($path) {
        $pos = strrpos(__FILE__, $_SERVER['PHP_SELF']);
        return substr(realpath($path), $pos);
    }
    
    // where
    __FILE__                         == "/Users/me/dev/web/repo1/example.php"
    $_SERVER['PHP_SELF']             ==              "/web/repo1/example.php"
    realpath("./some/relative.file") == "/Users/me/dev/web/repo1/some/relative.file"
    // If I cut off the pre-fix part from realpath($path), 
    // the remainder will be full path relative to virtual host root
    path2url("./some/relative.file") ==              "/web/repo1/some/relative.file"
    

    I think it's good practice to fore-prevent the potential bugs even we are not likely to use symlinks in production environment.

    0 讨论(0)
提交回复
热议问题