By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,439 Members | 1,641 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,439 IT Pros & Developers. It's quick & easy.

Canonical method for getting path from doucment root for a file?

P: n/a
Hello,

If you have the whole server path for a file is there a canonical way to get
the path from document root for that file so that you can present the file
ina browser or for download? Check $_SERVER['document_root'] and parse the
path?

My thought is that given OS diversity and individual server differences (eg
www vs public_html vs htdocs) that there may be no one-size-fits-all
solution but I thought I would throw this out there any way.

Thanks for any advice,

jg
Jul 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Hi

I don't even know, if I understood this, but here's my solution:

str_replace the string in the file-path that's equal to the doc-root by ""
or by "/" (or by the server-name; well this requires a second look at the
problem)... done

$file_name =
str_replace(dirname($_SERVER["DOCUMENT_ROOT"])."/","",dirname($file_name)."/
").basename($file_name);

// the over-use of dirname() is just to prevent OS-specific
// problems with the file-system

guess this only works, if the file is in a folder that's reachable from
outside the server (otherwise it would be sensless).
Jul 17 '05 #2

P: n/a
On Fri, 17 Sep 2004 11:57:39 -0500, "jerrygarciuh"
<de*****@no.spam.nolaflash.com> wrote:
Hello,

If you have the whole server path for a file is there a canonical way to get
the path from document root for that file so that you can present the file
ina browser or for download? Check $_SERVER['document_root'] and parse the
path?

My thought is that given OS diversity and individual server differences (eg
www vs public_html vs htdocs) that there may be no one-size-fits-all
solution but I thought I would throw this out there any way.

Thanks for any advice,

jg

If I understand your question correctly, you could use something like:
$path = dirname($_SERVER['PHP_SELF']);
If the URI is 'foo.com/bar/baz/page.php', $path will be '/bar/baz'.
HTH =)

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/
Jul 17 '05 #3

P: n/a
Ian,

Thanks for the reply!

What I am trying to do is take data that looks like these:

$fullPath = '/home/sumsite/public_html/facilities/vDocs/1/Clown.jpg';
$fullPath = '/home/sumsite/public_html/facilities/Dog.jpg';

and parse them uniformly into

$webPath = /facilities/vDocs/1/Clown.jpg';
$webPath = /facilities/Dog.jpg';

So they can be accessed by a browser. PHP_SELF in these case won't relate
to the dir in question. Maybe I can use chdir to my advantage here...

Thanks for looking,

jg

"Ian.H" <ia*@WINDOZEdigiserv.net> wrote in message
news:mt********************************@4ax.com...
On Fri, 17 Sep 2004 11:57:39 -0500, "jerrygarciuh"
<de*****@no.spam.nolaflash.com> wrote:
Hello,

If you have the whole server path for a file is there a canonical way to getthe path from document root for that file so that you can present the fileina browser or for download? Check $_SERVER['document_root'] and parse thepath?

My thought is that given OS diversity and individual server differences (egwww vs public_html vs htdocs) that there may be no one-size-fits-all
solution but I thought I would throw this out there any way.

Thanks for any advice,

jg

If I understand your question correctly, you could use something like:
$path = dirname($_SERVER['PHP_SELF']);
If the URI is 'foo.com/bar/baz/page.php', $path will be '/bar/baz'.
HTH =)

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #4

P: n/a
CR,

Thanks for the reply! Your code takes

$fullPath = '/home/sumsite/public_html/facilities/vDocs/1/Clown.jpg';

and returns "/";

I need it to return "/facilities/vDocs/1/Clown.jpg"; so a browser can get
the file.

Any advice?

TIA!

jg
"Christopher-Robin" <Ch***************@gmx.de> wrote in message
news:ci*************@news.t-online.com...
Hi

I don't even know, if I understood this, but here's my solution:

str_replace the string in the file-path that's equal to the doc-root by ""
or by "/" (or by the server-name; well this requires a second look at the
problem)... done

$file_name =
str_replace(dirname($_SERVER["DOCUMENT_ROOT"])."/","",dirname($file_name)."/ ").basename($file_name);

// the over-use of dirname() is just to prevent OS-specific
// problems with the file-system

guess this only works, if the file is in a folder that's reachable from
outside the server (otherwise it would be sensless).

Jul 17 '05 #5

P: n/a
Sorry, was testing carelessly.

This test:

$file_name = '/home/sumsite/public_html/facilities/vDocs/1/Clown.jpg';

echo $_SERVER["DOCUMENT_ROOT"] . '<p>';

echo $file_name =
str_replace(dirname($_SERVER["DOCUMENT_ROOT"])."/","",dirname($file_name)."/
").basename($file_name);
Prints:

/usr/local/apache/htdocs
/home/sumsite/public_html/facilities/vDocs/1/Clown.jpg

Your code looks good, I just have to figure out the doc root issue.

Thanks,

jg

"jerrygarciuh" <de*****@no.spam.nolaflash.com> wrote in message
news:_BG2d.25404$ni.8768@okepread01...
CR,

Thanks for the reply! Your code takes

$fullPath = '/home/sumsite/public_html/facilities/vDocs/1/Clown.jpg';

and returns "/";

I need it to return "/facilities/vDocs/1/Clown.jpg"; so a browser can get
the file.

Any advice?

TIA!

jg
"Christopher-Robin" <Ch***************@gmx.de> wrote in message
news:ci*************@news.t-online.com...
Hi

I don't even know, if I understood this, but here's my solution:

str_replace the string in the file-path that's equal to the doc-root by "" or by "/" (or by the server-name; well this requires a second look at the problem)... done

$file_name =

str_replace(dirname($_SERVER["DOCUMENT_ROOT"])."/","",dirname($file_name)."/
").basename($file_name);

// the over-use of dirname() is just to prevent OS-specific
// problems with the file-system

guess this only works, if the file is in a folder that's reachable from
outside the server (otherwise it would be sensless).


Jul 17 '05 #6

P: n/a
On Fri, 17 Sep 2004 11:57:39 -0500, "jerrygarciuh"
<de*****@no.spam.nolaflash.com> wrote:
If you have the whole server path for a file is there a canonical way to get
the path from document root for that file so that you can present the file
ina browser or for download? Check $_SERVER['document_root'] and parse the
path?

My thought is that given OS diversity and individual server differences (eg
www vs public_html vs htdocs) that there may be no one-size-fits-all
solution but I thought I would throw this out there any way.


There may be multiple URLs that map to the same filesystem file - do you want
all of them, or just one? A method to do this properly would be a bit of
challenge as it would require some understanding of the webserver's
configuration (whichever webserver you're using).

If you're just dealing with the base case of one document root covering all
accessible files, then the Christopher-Robin's post looks like the way to go.

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #7

P: n/a
Well,

This is the best I have been able to do. FAR from a global solution but
looks like it would work a lot of the time:

$path = '/home/someGuy/public_html/facilities/vDocs/1/Clown.jpg';
$pathAry = preg_split('/public_html|www/', $path);
echo $pathAry[1];

Thanks for the replies!

jg

"jerrygarciuh" <de*****@no.spam.nolaflash.com> wrote in message
news:eIE2d.25393$ni.20060@okepread01...
Hello,

If you have the whole server path for a file is there a canonical way to get the path from document root for that file so that you can present the file
ina browser or for download? Check $_SERVER['document_root'] and parse the path?

My thought is that given OS diversity and individual server differences (eg www vs public_html vs htdocs) that there may be no one-size-fits-all
solution but I thought I would throw this out there any way.

Thanks for any advice,

jg

Jul 17 '05 #8

P: n/a
try $_SERVER["SCRIPT_FILENAME"] or $_SERVER["PATH_TRANSLATED"] instead of
$_SERVER["DOCUMENT_ROOT"]

well, im not sure (its already 4am here)
there are many pre-defined variables in $_SERVER; just do an echo-loop to
check which is suitable

foreach($_SERVER as $i => $j)
echo "$i => $j<br>\n";
Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.