How do I get the filename without the params?

2019-02-23 02:23发布

I need to find the file name of the file I've 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 file.php returned

what I have found:

basename($_SERVER['REQUEST_URI'])

which returns:

file.php?a=b&c=d

in my case: I'm using the filename in a form in the section for my cart, so at the moment each time you click the 'reduce number of product' button it adds the GET params of the form (productId and action) to the end of the URL: file.php?a=b&c=d?a=b&c=d?a=b&c=d?a=b&c=d...

I know I could simply explode or something similar on '?'

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

and use the first part of the array, but I seem to recall something easier, however cannot locate the code again..

I'm new to PHP so explanation on your code would be appreciated.

Thanks, V

标签: php filenames
8条回答
狗以群分
2楼-- · 2019-02-23 02:53

Maybe you are looking for $_SERVER['PHP_SELF'].

Please notice that it has a '/' char in the beginning.

查看更多
贼婆χ
3楼-- · 2019-02-23 02:57
basename(parse_url($url,PHP_URL_PATH));

parse_url($url,PHP_URL_PATH) erase params. basename() get filename of path.

查看更多
登录 后发表回答