Php Display Current Server Path

Php Display Current Server Path

I need to setup a path in my php but I currently don't know the path.

I need to configure the paths to the uploads directory

Should look like this below:

/srv/www/uploads/

My uploads.php file is in the root...so

www/uploads/ ???

Is there anyway that I could get php to tell me my current path?

1

5 Answers

If you call getcwd it should give you the path:

<?php
  echo getcwd();
?>
echo $_SERVER["DOCUMENT_ROOT"];

'DOCUMENT_ROOT' The document root directory under which the current script is executing, as defined in the server's configuration file.

  • To get your current working directory: getcwd() (documentation)
  • To get the document root directory: $_SERVER['DOCUMENT_ROOT'] (documentation)
  • To get the filename of the current script: $_SERVER['SCRIPT_FILENAME']

You can also use the following alternative realpath.

Create a file called path.php

Put the following code inside by specifying the name of the created file.

<?php 
    echo realpath('path.php'); 
?>

A php file that you can move to all your folders to always have the absolute path from where the executed file is located.

;-)

php can call command line operations so

echo exec("pwd");
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sarah Jenkins
Author

Sarah Jenkins

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.