473,748 Members | 4,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
8 3005
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(dir name($_SERVER["DOCUMENT_R OOT"])."/","",dirname($f ile_name)."/
").basename($fi le_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
On Fri, 17 Sep 2004 11:57:39 -0500, "jerrygarci uh"
<de*****@no.spa m.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($_SERVE R['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
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*@WINDOZEdig iserv.net> wrote in message
news:mt******** *************** *********@4ax.c om...
On Fri, 17 Sep 2004 11:57:39 -0500, "jerrygarci uh"
<de*****@no.spa m.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($_SERVE R['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
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
"Christophe r-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(dir name($_SERVER["DOCUMENT_R OOT"])."/","",dirname($f ile_name)."/ ").basename($fi le_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
Sorry, was testing carelessly.

This test:

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

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

echo $file_name =
str_replace(dir name($_SERVER["DOCUMENT_R OOT"])."/","",dirname($f ile_name)."/
").basename($fi le_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

"jerrygarci uh" <de*****@no.spa m.nolaflash.com > wrote in message
news:_BG2d.2540 4$ni.8768@okepr ead01...
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
"Christophe r-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(dir name($_SERVER["DOCUMENT_R OOT"])."/","",dirname($f ile_name)."/
").basename($fi le_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
On Fri, 17 Sep 2004 11:57:39 -0500, "jerrygarci uh"
<de*****@no.spa m.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.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #7
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

"jerrygarci uh" <de*****@no.spa m.nolaflash.com > wrote in message
news:eIE2d.2539 3$ni.20060@okep read01...
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
try $_SERVER["SCRIPT_FILENAM E"] or $_SERVER["PATH_TRANSLATE D"] instead of
$_SERVER["DOCUMENT_R OOT"]

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($_SERVE R as $i => $j)
echo "$i => $j<br>\n";
Jul 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5081
by: gwen | last post by:
Hi, I have a code to copy/move folders to a specified folder on the root drive - ie c:\stuff However, I also have the folder "stuff" on the root of other partitions as well - ie d:\stuff, e:\stuff, etc. I would like the code to work so that the selected folder for moving/copying will always go to the "stuff" folder of the drive that it is on. In other words, if a folder is selected on the d drive, it will be moved to d:\stuff, if it's...
0
5729
by: Ben Wan | last post by:
Can anyone please tell me what is canonical path? and why does it need to throws IOexception? how come getAbsolutePath() doesn't return "c:\folder\folder" but getCanonicalPath() does? things I tried using java.io.File file.getName(); // return "." file.getAbsolutePath(); // return "." file.getCanonicalPath(); // return "c:\folder\folder" Thank You Very Much!
4
3311
by: | last post by:
Is it possible to include my remote web server path eg: m:/html/root/site1 | m:/html/root/site2 etc ....in virtual includes.... Thus eliminating the need to create duplicate INC files in each sub-domain on my remote web server host domain IP. eg: http://72.3.5.6.0 (www.site1.com; www.site2.com) I would like to be able to use a UNIVERSAL INC file in the root of my web IP
11
2024
by: KarimL | last post by:
Thanks for your advices... but i need to get the Image height because i dynamically resize the height of my webcontrol based on the image height. More i just have the url (relative parth) to the the image but in design time mode all variables concerning Server or Context ar not set ! ...so I can't use MapPath function to obtain the physical parth of the picture ... So my second question is how to retrieve the physical root path of the...
2
5286
by: Praveen | last post by:
Hi All, I have made a webservice in C# and it works fine in my machine. I ran into a crazy problem when I wanted to deploy it in windows 2003 server. I have run "aspnet_regiis.exe -i" to make sure that the extensions for .asmx file etc are in place. I am getting http 404 when I give the url for the asmx file. the http error code is wrong because I am dead sure that the file is there. could you please let me know what else needs to be...
0
3714
by: ruju00 | last post by:
I am getting an error in Login() method of the following class FtpConnection public class FtpConnection { public class FtpException : Exception { public FtpException(string message) : base(message){} public FtpException(string message, Exception innerException) : base(message,innerException){}
5
2656
by: EP | last post by:
This inquiry may either turn out to be about the suitability of the SHA-1 (160 bit digest) for file identification, the sha function in Python ... or about some error in my script. Any insight appreciated in advance. I am trying to reduce duplicate files in storage at home - I have a large number files (e.g. MP3s) which have been stored on disk multiple times under different names or on different paths. The using applications will...
7
2086
by: Hitesh | last post by:
Hi, I have a small script here that goes to inside dir and sorts the file by create date. I can return the create date but I don't know how to find the name of that file... I need file that is not latest but was created before the last file. Any hints... I am newbiw python dude and still trying to figure out lot of 'stuff'..
13
2810
by: lawpoop | last post by:
Hello all - I have a two part question. First of all, I have a website under /home/user/www/. The index.php and all the other website pages are under /home/user/www/. For functions that are used in multiple files, I have php files under / home/user/www/functions/. These files simply have So, in index.php and other files, I have
0
8984
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9530
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9312
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8237
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.