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
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.

Link to comment
Share on other sites

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/"

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

Yes, shared hosting provided by my school.

 

No, I definitely can't change any configuration. I don't understand why the DOCUMENT_ROOT is so different from what I expect, but is there really no way to accomplish what I am trying to do?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.