Find file name from string with php

public/images/portfolio/i-vis/1.jpg

How can I delete the whole path no matter what file name php uses?

+5
source share
3 answers

Take a look basename()

$path = 'public/images/portfolio/i-vis/1.jpg'
$name = basename($path); // $name == '1.jpg'

Also dirname()retrieves another part

$dir = dirname($path); // $dir == 'public/images/portfolio/i-vis'

If you need more additional information - pathinfo()

$info = pathinfo($path);
var_dump($info);

produces

array(4) {
    ["dirname"]=>
    string(29) "public/images/portfolio/i-vis"
    ["basename"]=>
    string(5) "1.jpg"
    ["extension"]=>
    string(3) "jpg"
    ["filename"]=>
    string(1) "1"
}

So $info['filename']provides you a file without extension.

+17
source
echo basename($string);

Take a look at the basename function .

+2
source

alternative solution. Just a bunch of explosions

$str='public/images/portfolio/i-vis/1.jpg';
$s = end(explode("/",$str));
print "filename " . $s."\n";
$e = explode(".", $s );
print "without extension: $e[0]\n";
+1
source

All Articles