Connecting Tech Pros Worldwide Forums | Help | Site Map

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

jerrygarciuh
Guest
 
Posts: n/a
#1: Jul 17 '05
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



Christopher-Robin
Guest
 
Posts: n/a
#2: Jul 17 '05

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


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


Ian.H
Guest
 
Posts: n/a
#3: Jul 17 '05

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


On Fri, 17 Sep 2004 11:57:39 -0500, "jerrygarciuh"
<designs@no.spam.nolaflash.com> wrote:
[color=blue]
>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
>[/color]


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/
jerrygarciuh
Guest
 
Posts: n/a
#4: Jul 17 '05

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


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" <ian@WINDOZEdigiserv.net> wrote in message
news:mtcmk01dao6kemejqnlutmr573bb83g8s0@4ax.com...[color=blue]
> On Fri, 17 Sep 2004 11:57:39 -0500, "jerrygarciuh"
> <designs@no.spam.nolaflash.com> wrote:
>[color=green]
> >Hello,
> >
> >If you have the whole server path for a file is there a canonical way to[/color][/color]
get[color=blue][color=green]
> >the path from document root for that file so that you can present the[/color][/color]
file[color=blue][color=green]
> >ina browser or for download? Check $_SERVER['document_root'] and parse[/color][/color]
the[color=blue][color=green]
> >path?
> >
> >My thought is that given OS diversity and individual server differences[/color][/color]
(eg[color=blue][color=green]
> >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
> >[/color]
>
>
> 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/[/color]


jerrygarciuh
Guest
 
Posts: n/a
#5: Jul 17 '05

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


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" <Christopher-Robin@gmx.de> wrote in message
news:cif8pb$u45$04$1@news.t-online.com...[color=blue]
> 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 =
>[/color]
str_replace(dirname($_SERVER["DOCUMENT_ROOT"])."/","",dirname($file_name)."/[color=blue]
> ").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).
>
>[/color]


jerrygarciuh
Guest
 
Posts: n/a
#6: Jul 17 '05

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


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" <designs@no.spam.nolaflash.com> wrote in message
news:_BG2d.25404$ni.8768@okepread01...[color=blue]
> 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" <Christopher-Robin@gmx.de> wrote in message
> news:cif8pb$u45$04$1@news.t-online.com...[color=green]
> > 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[/color][/color]
""[color=blue][color=green]
> > or by "/" (or by the server-name; well this requires a second look at[/color][/color]
the[color=blue][color=green]
> > problem)... done
> >
> > $file_name =
> >[/color]
>[/color]
str_replace(dirname($_SERVER["DOCUMENT_ROOT"])."/","",dirname($file_name)."/[color=blue][color=green]
> > ").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).
> >
> >[/color]
>
>[/color]


Andy Hassall
Guest
 
Posts: n/a
#7: Jul 17 '05

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


On Fri, 17 Sep 2004 11:57:39 -0500, "jerrygarciuh"
<designs@no.spam.nolaflash.com> wrote:
[color=blue]
>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.[/color]

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 / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
jerrygarciuh
Guest
 
Posts: n/a
#8: Jul 17 '05

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


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" <designs@no.spam.nolaflash.com> wrote in message
news:eIE2d.25393$ni.20060@okepread01...[color=blue]
> Hello,
>
> If you have the whole server path for a file is there a canonical way to[/color]
get[color=blue]
> 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[/color]
the[color=blue]
> path?
>
> My thought is that given OS diversity and individual server differences[/color]
(eg[color=blue]
> 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
>
>[/color]


Christopher-Robin
Guest
 
Posts: n/a
#9: Jul 17 '05

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


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


Closed Thread