67

I want to get filename without any $_GET variable values from a URL in php?

My URL is http://learner.com/learningphp.php?lid=1348

I only want to retrieve the learningphp.php from the URL?

How to do this?

I used basename() function but it gives all the variable values also: learntolearn.php?lid=1348 which are in the URL.

Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64
sqlchild
  • 8,754
  • 28
  • 105
  • 167

13 Answers13

84

This should work:

echo basename($_SERVER['REQUEST_URI'], '?' . $_SERVER['QUERY_STRING']);

But beware of any malicious parts in your URL.

str
  • 42,689
  • 17
  • 109
  • 127
  • 2
    Okay, but the question mark is still output. This works fine for me: `echo basename($_SERVER['REQUEST_URI'], '?'.$_SERVER['QUERY_STRING']);`. – ComFreek Oct 21 '11 at 16:40
  • Thanks, I was unsure whether the question mark was included or not in `QUERY_STRING`. Now the solution works. – str Oct 21 '11 at 16:42
  • 1
    But not for this URL: `http://www.example.com/path/script.php/test/?arg1=val1` ;) – ComFreek Oct 21 '11 at 16:43
41

Following steps shows total information about how to get file, file with extension, file without extension. This technique is very helpful for me. Hope it will be helpful to you too.

  $url = 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png';
        $file = file_get_contents($url); // to get file
        $name = basename($url); // to get file name
        $ext = pathinfo($url, PATHINFO_EXTENSION); // to get extension
        $name2 =pathinfo($url, PATHINFO_FILENAME); //file name without extension
  • 3
    I don't understand why this has so many upvotes, it doesn't answer the question. If there is a query string on the end of the URL, it's included with the results here. OP wants the filename without the query string. – jessica May 27 '20 at 03:09
  • 2
    @jessica I know this is not the actual answer to the question so it's not a marked solution too. People are searching for various kinds of URLs when they are searching for URL like provided by me they are upvoting my ans. – Khadka Pushpendra Jun 04 '20 at 09:41
34

Is better to use parse_url to retrieve only the path, and then getting only the filename with the basename. This way we also avoid query parameters.

<?php

// url to inspect
$url = 'http://www.example.com/image.jpg?q=6574&t=987';

// parsed path
$path = parse_url($url, PHP_URL_PATH);

// extracted basename
echo basename($path);

?>

Is somewhat similar to Sultan answer excepting that I'm using component parse_url parameter, to obtain only the path.

Fipsi
  • 670
  • 1
  • 9
  • 21
foxtrot
  • 1,056
  • 1
  • 8
  • 24
24

Your URL:

$url = 'http://learner.com/learningphp.php?lid=1348';
$file_name = basename(parse_url($url, PHP_URL_PATH));
echo $file_name;

output: learningphp.php

Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64
22

Use parse_url() as Pekka said:

<?php
$url = 'http://www.example.com/search.php?arg1=arg2';

$parts = parse_url($url);

$str = $parts['scheme'].'://'.$parts['host'].$parts['path'];

echo $str;
?>

http://codepad.org/NBBf4yTB

In this example the optional username and password aren't output!

ComFreek
  • 29,044
  • 18
  • 104
  • 156
10

You can use,

$directoryURI =basename($_SERVER['SCRIPT_NAME']);

echo $directoryURI;
Shahzad Barkati
  • 2,532
  • 6
  • 25
  • 33
SwR
  • 612
  • 1
  • 8
  • 21
  • This gives you an incorrect result if the running script name is not exactly the one called in the uri (e.g. using url_rewrite) – Steve Horvath Mar 23 '21 at 04:42
6

An other way to get only the filename without querystring is by using parse_url and basename functions :

$parts = parse_url("http://example.com/foo/bar/baz/file.php?a=b&c=d");
$filename = basename($parts["path"]); // this will return 'file.php'
ᴄʀᴏᴢᴇᴛ
  • 2,939
  • 26
  • 44
  • this turns into 98f3da9ae1c4dd972785fde37d21d3a9.jpg__ why the __ at the end ? edit: I used trim and fixed it – Jaxx0rr Nov 22 '18 at 03:37
5

Try the following code:

For PHP 5.4.0 and above:

$filename = basename(parse_url('http://learner.com/learningphp.php?lid=1348')['path']);

For PHP Version < 5.4.0

$parsed = parse_url('http://learner.com/learningphp.php?lid=1348');
$filename = basename($parsed['path']);
Sultan
  • 637
  • 6
  • 14
4
$filename = pathinfo( parse_url( $url, PHP_URL_PATH ), PATHINFO_FILENAME ); 

Use parse_url to extract the path from the URL, then pathinfo returns the filename from the path

Al Foиce ѫ
  • 4,195
  • 12
  • 39
  • 49
James
  • 41
  • 1
  • please format your answer appropriately (use backticks for inline code and 4-space indentation for blocks of code) – YakovL Oct 13 '16 at 20:41
2

The answer there assumes you know that the URL is coming from a request, which it may very well not be. The generalized answer would be something like:

$basenameWithoutParameters = explode('?', pathinfo($yourURL, PATHINFO_BASENAME))[0];

Here it just takes the base path, and splits out and ignores anything ? and after.

Jeremy L.
  • 853
  • 8
  • 15
1

Use this function:

function getScriptName()
{
    $filename = baseName($_SERVER['REQUEST_URI']);
    $ipos = strpos($filename, "?");
    if ( !($ipos === false) )   $filename = substr($filename, 0, $ipos);
    return $filename;
}
1
$url = "learner.com/learningphp.php?lid=1348";
$l = parse_url($url);
print_r(stristr($l['path'], "/"));
Mob
  • 10,958
  • 6
  • 41
  • 58
0

May be i am late

$e = explode("?",basename($_SERVER['REQUEST_URI']));
$filename = $e[0];
Rachit Vohera
  • 707
  • 7
  • 10