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:
test test.php test.php?hey=there hey=there https://example.com /home/myserver/public_html /somefolder/anotherfolder
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
<?php // filename only including extension but no path $script_name_with_ext = basename(__FILE__); // filename only with no extension and no path $script_name_no_ext = basename(__FILE__, ".php"); // filename only including extension and all arguments but no path $script_name_with_args = basename($_SERVER["REQUEST_URI"]); // arguments only with no filename $args_no_script_name = str_replace ( $script_name_with_ext."?" , "", $script_name_with_args); // path portion only with no domain and no server portions $path_only = pathinfo($_SERVER['PHP_SELF'], 1); // domain url portion only with no path $domain_no_path = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://{$_SERVER['HTTP_HOST']}"; // domain url including path $domain_with_path = $domain_no_path.$path_only; // domain url and path and filename with extension $full_domain = $domain_with_path."/".$script_name_with_ext; // server including path $server_with_path = str_replace ("/".$script_name_with_ext , "", __FILE__); // server only with no path $server_no_path = str_replace($path_only, "", $server_with_path); // server and path and filename with extension $full_server = $server_with_path."/".$script_name_with_ext; // output everything here echo $script_name_no_ext."<br>"; echo $script_name_with_ext."<br>"; echo $script_name_with_args."<br>"; echo $args_no_script_name."<br><br>"; echo $path_only."<br><br>"; echo $server_no_path."<br>"; echo $server_with_path."<br>"; echo $full_server."<br>"; echo $full_server."?".$args_no_script_name."<br><br>"; echo $domain_no_path."<br>"; echo $domain_with_path."<br>"; echo $full_domain."<br>"; echo $full_domain."?".$args_no_script_name."<br><br>"; ?>
The output will look like this:
test test.php test.php?hey=there hey=there /somefolder/anotherfolder /home/myserver/public_html /home/myserver/public_html/somefolder/anotherfolder /home/myserver/public_html/somefolder/anotherfolder/test.php /home/myserver/public_html/somefolder/anotherfolder/test.php?hey=there https://example.com https://example.com/somefolder/anotherfolder https://example.com/somefolder/anotherfolder/test.php https://example.com/somefolder/anotherfolder/test.php?hey=there