PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteUse parse_url() to separate a complete URL into its path and query, then pass only the path to basename() to get its final segment. Append the query separately if you need the filename and the full query string together. For the current incoming request, start with $_SERVER['REQUEST_URI'].
Get the final filename and preserve its query string
This returns the final path segment followed by the original query string, keeping percent-encoded values such as %20 and %27 intact:
As an Amazon Associate I earn from qualifying purchases.
<?php
$url = 'https://www.domain.com.au/dev/website/shop-product.php?shopCategoryID=1&shopSubCategoryID=2&productID=1&title=Buy%20Blank%20T-Shirts%20Online&category=Men%27s%20T-Shirts';
$parts = parse_url($url);
$path = $parts['path'] ?? '';
$filename = basename($path);
$query = $parts['query'] ?? null;
$result = $filename . ($query !== null ? '?' . $query : '');
echo $result;
?>
The result is shop-product.php?shopCategoryID=1&shopSubCategoryID=2&productID=1&title=Buy%20Blank%20T-Shirts%20Online&category=Men%27s%20T-Shirts. The null check means no question mark is added when the URL has no query.
parse_url() separates URL components; it does not validate that a URL is well formed or safe. Its query value remains encoded rather than being decoded.
#1 Best Overall
Handle missing or unusable paths
A URL may have no path, end in a slash, or contain a query without a filename. If input may be malformed, check that parsing succeeded and that the parsed path is non-empty before using the result. An empty final segment is not a filename to rely on.
Get the filename or extension without the query
If you want only the last path segment, use basename() on the parsed path:
Rank #2
$filename = basename(parse_url($url, PHP_URL_PATH) ?? '');
For an extension, pass that filename to pathinfo():
Free tools Windows power users keep installed
One-click scans. No signup required.
$extension = pathinfo($filename, PATHINFO_EXTENSION);
basename() returns the trailing component of a path. pathinfo() can also provide filename and basename details. PATHINFO_EXTENSION is the text after the last dot: archive.tar.gz has extension gz, not tar.gz. If your application treats compound suffixes as meaningful, define that convention separately.
Read the current request URL in PHP
When handling the request currently reaching your server, use the request target already provided by PHP:
$requestTarget = $_SERVER['REQUEST_URI'] ?? '';
REQUEST_URI contains the request path and query, for example /dev/website/shop-product.php?productID=1. If you need the final segment plus query, parse this value into path and query, then apply basename() to the path only. If you need the complete request target, use it directly; rebuilding a URL from host and scheme is unnecessary.
Rank #4
The PHP $_SERVER documentation describes values supplied by the server and execution environment. Do not treat request-derived values as trusted filesystem paths.
Recommended Free Tools
Quick Recap
Keep URL parsing separate from file validation
- A URL path can point to a rewritten route or controller rather than a physical file, so a visible filename or extension does not establish what the server returns.
- A fragment beginning with
#is separate from path and query. It is not part of the normal HTTP request target sent to the server. - Keep percent escapes when exact URL representation matters. Decode only for a defined application need; decoding is not validation and can change how separators are interpreted.
- For uploads, remote files, or filesystem operations, a filename extension is untrusted input and does not prove a file’s type or safety. Validate based on the operation you intend to perform.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




