omg
omg

Reputation: 139832

How do I get a file name from a full path with PHP?

For example, how do I get Output.map

from

F:\Program Files\SSH Communications Security\SSH Secure Shell\Output.map

with PHP?

Upvotes: 281

Views: 484049

Answers (14)

Metafaniel
Metafaniel

Reputation: 30604

I've done this using the function PATHINFO which creates an array with the parts of the path for you to use! For example, you can do this:

<?php
    $xmlFile = pathinfo('/usr/admin/config/test.xml');

    function filePathParts($arg1) {
        echo $arg1['dirname'], "\n";
        echo $arg1['basename'], "\n";
        echo $arg1['extension'], "\n";
        echo $arg1['filename'], "\n";
    }

    filePathParts($xmlFile);
?>

This will return:

/usr/admin/config
test.xml
xml
test

The use of this function has been available since PHP 5.2.0!

Then you can manipulate all the parts as you need. For example, to use the full path, you can do this:

$fullPath = $xmlFile['dirname'] . '/' . $xmlFile['basename'];

Upvotes: 90

Chandni Soni
Chandni Soni

Reputation: 387

$image_path = "F:\Program Files\SSH Communications Security\SSH Secure Shell\Output.map";
$arr = explode('\\',$image_path);
$name = end($arr);

Upvotes: 1

Khadka Pushpendra
Khadka Pushpendra

Reputation: 999

There are several ways to get the file name and extension. You can use the following one which is easy to use.

$url = 'http://www.nepaltraveldoor.com/images/trekking/nepal/annapurna-region/Annapurna-region-trekking.jpg';
$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

Upvotes: 19

Kathir
Kathir

Reputation: 1240

It's simple. For example:

<?php
    function filePath($filePath)
    {
        $fileParts = pathinfo($filePath);

        if (!isset($fileParts['filename']))
        {
            $fileParts['filename'] = substr($fileParts['basename'], 0, strrpos($fileParts['basename'], '.'));
        }
        return $fileParts;
    }

    $filePath = filePath('/www/htdocs/index.html');
    print_r($filePath);
?>

The output will be:

Array
(
    [dirname] => /www/htdocs
    [basename] => index.html
    [extension] => html
    [filename] => index
)

Upvotes: 2

internals-in
internals-in

Reputation: 5038

With SplFileInfo:

SplFileInfo The SplFileInfo class offers a high-level object oriented interface to information for an individual file.

Ref: http://php.net/manual/en/splfileinfo.getfilename.php

$info = new SplFileInfo('/path/to/foo.txt');
var_dump($info->getFilename());

o/p: string(7) "foo.txt"

Upvotes: 15

Sun Junwen
Sun Junwen

Reputation: 1098

basename() has a bug when processing Asian characters like Chinese.

I use this:

function get_basename($filename)
{
    return preg_replace('/^.+[\\\\\\/]/', '', $filename);
}

Upvotes: 9

chandoo
chandoo

Reputation: 1316

To get the exact file name from the URI, I would use this method:

<?php
    $file1 =basename("http://localhost/eFEIS/agency_application_form.php?formid=1&task=edit") ;

    //basename($_SERVER['REQUEST_URI']); // Or use this to get the URI dynamically.

    echo $basename = substr($file1, 0, strpos($file1, '?'));
?>

Upvotes: 3

Vertigo
Vertigo

Reputation: 2746

You can use the basename() function.

Upvotes: 5

Pascal MARTIN
Pascal MARTIN

Reputation: 400912

The basename function should give you what you want:

Given a string containing a path to a file, this function will return the base name of the file.

For instance, quoting the manual's page:

<?php
    $path = "/home/httpd/html/index.php";
    $file = basename($path);         // $file is set to "index.php"
    $file = basename($path, ".php"); // $file is set to "index"
?>

Or, in your case:

$full = 'F:\Program Files\SSH Communications Security\SSH Secure Shell\Output.map';
var_dump(basename($full));

You'll get:

string(10) "Output.map"

Upvotes: 14

Mark Rushakoff
Mark Rushakoff

Reputation: 258128

You're looking for basename.

The example from the PHP manual:

<?php
$path = "/home/httpd/html/index.php";
$file = basename($path);         // $file is set to "index.php"
$file = basename($path, ".php"); // $file is set to "index"
?>

Upvotes: 535

antelove
antelove

Reputation: 3348

<?php

  $windows = "F:\Program Files\SSH Communications Security\SSH Secure Shell\Output.map";

  /* str_replace(find, replace, string, count) */
  $unix    = str_replace("\\", "/", $windows);

  print_r(pathinfo($unix, PATHINFO_BASENAME));

?> 

body, html, iframe { 
  width: 100% ;
  height: 100% ;
  overflow: hidden ;
}
<iframe src="https://ideone.com/Rfxd0P"></iframe>

Upvotes: 0

Douglas Tober
Douglas Tober

Reputation: 300

To do this in the fewest lines I would suggest using the built-in DIRECTORY_SEPARATOR constant along with explode(delimiter, string) to separate the path into parts and then simply pluck off the last element in the provided array.

Example:

$path = 'F:\Program Files\SSH Communications Security\SSH SecureShell\Output.map'

//Get filename from path
$pathArr = explode(DIRECTORY_SEPARATOR, $path);
$filename = end($pathArr);

echo $filename;
>> 'Output.map'

Upvotes: 4

atwebceo
atwebceo

Reputation: 303

Try this:

echo basename($_SERVER["SCRIPT_FILENAME"], '.php') 

Upvotes: 12

p4bl0
p4bl0

Reputation: 3906

$filename = basename($path);

Upvotes: 8

Related Questions