How to get file name without parameters?

I need to find the name of the file that I included without the GET parameters.

e.g .: if the current URL is http://www.mysite.com/folder/file.php?a=b&c=d , I want to return the .php file

what i found:

basename($_SERVER['REQUEST_URI'])

which returns:

file.php?a=b&c=d

in my case: I use the name of the file in the form in the section for my cart, so every time you click the "reduce the number of products" button, it adds the GET parameters of the form (productId and action) to the end of the URL: file.php = B &? c = q a = b &? c = q a = b &? c = q a = b & c = q ...

I know I can just explode or something like that on '?'

$file = explode("?", basename($_SERVER['REQUEST_URI']))

and use the first part of the array, but I seem to be recalling something, but can't find the code again.

PHP, .

,

+5
9

parse_url. :

$url = parse_url($url, PHP_URL_PATH);

, - :

$url = explode('/', parse_url($url, PHP_URL_PATH));
$url = end($url);
+11

, $_SERVER['PHP_SELF'].

, "/" char.

+10

basename(__FILE__);? , , .

basename($_SERVER['SCRIPT_NAME']);. , , , .

+5
basename($_SERVER['SCRIPT_NAME']);

.

, . php .

<pre><?php
print_r($_SERVER);
?></pre>
+2
basename(parse_url($url,PHP_URL_PATH));

parse_url ($ url, PHP_URL_PATH) erases the parameters. basename () get the path file name.

+1
source

Take a look at some of the other values ​​in $_SERVER.

Maybe, $_SERVER["PHP_SELF"]or $_SERVER["SCRIPT_NAME"]will return what you want.

0
source

This is the closest I have found. Here is how I did it:

$url = parse_url($_SERVER['PHP_SELF'], PHP_URL_PATH);
$url = explode('/', parse_url($url, PHP_URL_PATH));
$url = end($url);
error_log($url);
0
source

Best solution for me:

$file = basename(parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH));
0
source

The only way to get the script name without parameters is to make a POST request

-1
source

All Articles