Jump to content

Convert URL to local path


inarius

Recommended Posts

What's the easiest way to convert a local URL, such as $_SERVER['PHP_SELF'], to a local file path?

 

I don't just want the path to the current file or directory. I wanted to find a (hopefully simple) function that would take any url on my server and give me the local file path it corresponds to.

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/41880-convert-url-to-local-path/
Share on other sites

function get_current_file() {
    return $_SERVER['DOCUMENT_ROOT'] . $_SERVER['PHP_SELF'];
}

Other than that I can't think of any purpose for trying to find that information out.  I guess you could do:

function get_local($url) {
    $urlParts = parse_url($url);
    return $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . $urlParts['path'];
}

Completely untested, so I'm not entirely sure how it'll work.

I have actually tested something like this. I am using shared webspace provided by my school, and when I tested the output of $_SERVER['DOCUMENT_ROOT'], I got something I didn't expect.

 

$_SERVER['DOCUMENT_ROOT'] returns "/usrweb2/netsite-docs"

 

__FILE__ returns "/d3/home/student/amb93/public_html/index.php"

 

All of this maps to a web address like http://www.hostname.com/~amb93/index.html

 

So, I would like a function that can take the URL http://www.hostname.com/~amb93/classwork/ and return "/d3/home/student/amb93/public_html/classwork/"

are you using a shared hosting package?

 

if so i think you wont get the current folders with any php built in function's

just for now your have to use the directory nam e you no ok.

 

unless but a big but the hosting company change there whole configreaction for you!

Document_root is the document root to your school's main web page (most likely).

 

If your URL is something like http://yourschool.com/~yourusername, it'll load files from some other drive on the computer, while the document_root still remains the root path for http://yourschool.com.

 

Example, http://yourschool.com may load from /usrweb2/netsite-docs

While http://yourschool.com/~yourusername may load from /d3/home/student/amb93/public_html/index.php

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.