Get Parent Directory Name in PHP

I am uploading a file with PHP and I have set the path of my upload directory as:
$uploadDirectory  = '../../../../../misc/uploads/my_images/'; 

Open in new window


I am running php script from a path like: c:/wampp/htdocs/imagework/mytemplates/final/uploadscript/uploadfile.php
the misc directory is in c:/wampp/htdocs/imagework/misc/uploads/my_images/

How can I fetch the name of my root directory which is 'imagework' so that I will replace the path of $uploadDirectory. I don't want to use ../../../ etc

I need to get the name of my directory which is 'imagework'
 I have tried $_SERVER['HTTP_HOST'], $_SERVER['DOCUMENT_ROOT'] but it gives me the path of localhost not for the imagework directory.

Looking forward for help

Thanks
Shano KhanSoftware EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
If you're running this through a web server, then you don't tend to deal with paths relative to the system - you deal with paths relative to your web root, which looks like it should be the imagework folder. If that's the case, then the root of your web is just a single slash
0
Julian HansenCommented:
I would use a constant for this.

define('IMAGE_PATH', 'C:/wampp/htdocs/imagework');

Open in new window

And then prepend that on to the files I want to save. This allows you to change your path without having to change the structure of your site.

If you want to find the path to the webroot of the site then you could do this
$actual = getcwd();
$virtual = substr(dirname($_SERVER['PHP_SELF']), 1);
$pos = stripos($actual, $virtual);
$path = substr($actual, 0, $pos);
echo $path;

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Shano KhanSoftware EngineerAuthor Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.