Skip to content
Advertisement

How to get current server path, url domain, filename or script name in PHP

When working with a script in PHP, I need to know what its current folder and path is, both from the server point of view and the domain url point of view.

I also need the filename or script name with and without the extension, and also with and without any arguments passed in.

For example, if I call the following:

https://example.com/somefolder/anotherfolder/test.php?hey=there

I want to get back all these individual parts:

JavaScript

Advertisement

Answer

So this is my generic solution that will break it all down and then allow you to piece everything back together as needed.

See output below for putting script at this following location (and then calling it): https://example.com/somefolder/anotherfolder/test.php?hey=there

JavaScript

The output will look like this:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement