PHP absolute server path to website (the easy and fastest way)

by
on June 8, 2010
(1 minute read)

Here’s a quick note for all those people wondering what the absolute path to their server is. There are many tools and solutions around the Internet that help you find it, as well as php_info() but all of them seem overcomplicated. The easiest and quickest way to do it is by using a simple stupid trick:

Please donate to keep this server up

Create a PHP file and add some invalid code such as random letters:

Now save your file and upload it to the web server root folder.

Open it with your browser, an error message like this should appear:

Parse error: syntax error, unexpected T_STRING in /home/xavi/hello/public_html/test.php on line 2

That’s it! The home path for this example is: /home/xavi/hello/public_html/.

Error not showing?

If you can’t see any errors you may need to enable the debugger by activating/displaying PHP error reporting.

Code to display PHP absolute path

The trick above is for when you can’t be bothered to look it up. If you have the time to copy and paste then this code here is the right one:

<?php
ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);
echo getcwd() . "\n";

That’s it, get those 4 lines of code and paste them in a new file.

Looking for cool gift ideas? TrendLiker.com lists hundreds of nice items and lets you know when they go on sale on Amazon.

Did you enjoy this article?

Grab me a coffee ☕

3 comments

  • John says:

    Brilliant! This solution is definitely great :)
    I have been looking for the PHP function but didn’t think that errors display the complete path to the file in the server.
    Cheers!

  • rocoso says:

    Thats super cool!
    You can also do this

    $script_directory = substr($_SERVER[‘SCRIPT_FILENAME’], 0, strrpos($_SERVER[‘SCRIPT_FILENAME’], ‘/’));

  • Luis says:

    And.. what about using the proper function to do this??

    // current directory
    echo getcwd() . "\n";

    http://php.net/manual/en/function.getcwd.php

    Saludos desde España ;)

Follow me for cool new products and interesting findings on graphic design, web development, marketing, startups, life and humor.


/*Twitter*/ !function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs"); /*Facebook (function(d, s, id) {var js, fjs = d.getElementsByTagName(s)[0];if (d.getElementById(id)) {return;}js = d.createElement(s); js.id = id;js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1&appId=28624667607";fjs.parentNode.insertBefore(js, fjs);}(document, 'script', 'facebook-jssdk'));*/ /*Google+*/ window.___gcfg = {lang: 'en-GB'};(function() {var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;po.src = 'https://apis.google.com/js/plusone.js';var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);})();
Airbnb 25€ discount