Path to the the folder above document root

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Markto
Forum Newbie
Posts: 22
Joined: Thu Nov 17, 2011 1:13 pm

Path to the the folder above document root

Post by Markto »

I am developing locally on windows in C:\web\www. My document root, as given by $_SERVER['DOCUMENT_ROOT'], is C:/web/www/ .

Many security tutorials for uploading files state that one should upload outside of their root directory, where apache cannot access it. So I guess this means C:/web/. However, I do not know how to specify that path. It would be even better if I could specify that path relative to the root. I want a variable that gives me the intended path to c:/web/ equivalent (so whatever is above the root, no matter what server I am on), and no matter where my file is. Can someone help me out here? ($path = c:/web/ is too static.)
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Path to the the folder above document root

Post by Celauran »

Code: Select all

$_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . '..';
Markto
Forum Newbie
Posts: 22
Joined: Thu Nov 17, 2011 1:13 pm

Re: Path to the the folder above document root

Post by Markto »

Thanks for all of your help Celauran!
Markto
Forum Newbie
Posts: 22
Joined: Thu Nov 17, 2011 1:13 pm

Re: Path to the the folder above document root

Post by Markto »

Hrm, I tried it, and it gave me this when echoed:

C:/web/www/\..
I tried to move a temp file there and got this error:
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\web\tmp\php1D00.tmp' to 'C:/web/www/\..'

I also tried:

Code: Select all

$_SERVER['DOCUMENT_ROOT'] . '..' . DIRECTORY_SEPARATOR
But got the same error, except the directory_separator appears in a more natural position:
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\web\tmp\php316B.tmp' to 'C:/web/www/..\'
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Path to the the folder above document root

Post by Celauran »

Try just using "/" instead of the DIRECTORY_SEPARATOR constant, then.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Path to the the folder above document root

Post by McInfo »

Both of these will give the directory above the document root.

Code: Select all

dirname($_SERVER['DOCUMENT_ROOT'])
realpath($_SERVER['DOCUMENT_ROOT'].'/..')
Markto
Forum Newbie
Posts: 22
Joined: Thu Nov 17, 2011 1:13 pm

Re: Path to the the folder above document root

Post by Markto »

That worked, thanks. However, I am still unable to move files to the directory. I am given an error that I am unable to move a file to that location (c:\web), although at least the location is right. Any ideas on why that might be? I am running on wamp, so my c:\web directory is my wamp directory. Could this be causing it?
Markto
Forum Newbie
Posts: 22
Joined: Thu Nov 17, 2011 1:13 pm

Re: Path to the the folder above document root

Post by Markto »

Can someone let me know what they think might be going wrong here?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Path to the the folder above document root

Post by McInfo »

Check that the file name is included in the destination path.
Markto
Forum Newbie
Posts: 22
Joined: Thu Nov 17, 2011 1:13 pm

Re: Path to the the folder above document root

Post by Markto »

You got it, thanks. I overlooked that, and thought it might have been some unseen file permissions error. You've really helped me out here - been setting this problem aside for a while now!
Post Reply