PHP - Convert File system path to URL

后端 未结 11 1489
野的像风
野的像风 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));
    
    0 讨论(0)
  • 2020-11-29 08:39

    For example, i used this one to convert C:\WAMP\WWW\myfolder\document.txt to http://example.com/myfolder/document.txt use this one:

    $file_path=str_replace('\\','/',$file_path);
    $file_path=str_replace($_SERVER['DOCUMENT_ROOT'],'',$file_path);
    $file_path='http://'.$_SERVER['HTTP_HOST'].$file_path;
    
    0 讨论(0)
  • 2020-11-29 08:40

    The code below is well commented:

    function pathToURL($path) {
      //Replace backslashes to slashes if exists, because no URL use backslashes
      $path = str_replace("\\", "/", realpath($path));
    
      //if the $path does not contain the document root in it, then it is not reachable
      $pos = strpos($path, $_SERVER['DOCUMENT_ROOT']);
      if ($pos === false) return false;
    
      //just cut the DOCUMENT_ROOT part of the $path
      return substr($path, strlen($_SERVER['DOCUMENT_ROOT']));
      //Note: usually /images is the same with http://somedomain.com/images,
      //      So let's not bother adding domain name here.
    }
    echo pathToURL('some/path/on/public/html');
    
    0 讨论(0)
  • 2020-11-29 08:44

    All answers here promotes str_replace() which replaces all occurences anywhere in the string, not just in the beginning. preg_replace() will make sure we only do an exact match from the beginning of the string:

    function remove_path($file, $path = UPLOAD_PATH) {
      return preg_replace("#^($path)#", '', $file);
    }
    

    Windows can be a problem where directory separators / and \. Make sure you replace the directory separators first:

    function remove_path($file, $path = UPLOAD_PATH) {
      $file = preg_replace("#([\\\\/]+)#", '/', $file);
      $path = preg_replace("#([\\\\/]+)#", '/', $path);
      return preg_replace("#^($path)#", '', $file);
    }
    

    I would play with something like the following. Make note of realpath() and rtrim().

    function webpath($file) {
      $document_root = rtrim(preg_replace("#([\\\\/]+)#", '/', $_SERVER['DOCUMENT_ROOT']), '/');
      $file = preg_replace("#([\\\\/]+)#", '/', realpath($file));
      return preg_replace("#^($document_root)#", '', $file);
    }
    
    echo webpath(__FILE__); // Returns webpath to self
    echo webpath('../file.ext'); // Relative paths
    echo webpath('/full/path/to/file.ext'); // Full paths
    
    0 讨论(0)
  • 2020-11-29 08:45

    More accurate way (including host port) would be to use this

    function path2url($file, $Protocol='http://') {
        return $Protocol.$_SERVER['HTTP_HOST'].str_replace($_SERVER['DOCUMENT_ROOT'], '', $file);
    }
    
    0 讨论(0)
  • 2020-11-29 08:49

    Make it easy on yourself and just define the correct locations for both the filesystem and web folders and prepend the image filename with them.

    Somewhere, you'd declare:

    define('PATH_IMAGES_FS', '/var/www/example.com/uploads/');
    define('PATH_IMAGES_WEB', 'uploads/');
    

    Then you can just swap between paths depending on your need:

    $image_file = 'myphoto.jpg';
    
    $file = PATH_IMAGES_FS.$image_file;
    //-- stores: /var/www/example.com/uploads/myphoto.jpg
    
    print PATH_IMAGES_WEB.$image_file;
    //-- prints: uploads/myphoto.jpg
    
    0 讨论(0)
提交回复
热议问题