Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find the filename from a string with php

Tags:

path

php

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

How could i remove all the path regardless of what the filename is using php?

like image 948
Andy Avatar asked Nov 27 '09 12:11

Andy


People also ask

How to get the filename in PHP?

You can use function basename() to get the filename without the path; or basename(path, suffix) to get the filename without the extension. For example, <? php var_dump($_SERVER['PHP_SELF']); // e.g., 'test/test.

How to find file path in PHP?

To get the exact path of file, then, you can use realpath(). In between the round brackets of the function, type the name of the file. $dir = dirname("folder/myphp/fileDir.

What does pathinfo do in PHP?

The pathinfo() function returns information about a file path.

What is the basename in PHP?

The basename is an in-built function in PHP, which returns the file name present at the path provided as an argument. Syntax: Below is the syntax to use the Basename function in PHP. String basename($path, $suffix) The function has two parameters, i.e., path and suffix.


2 Answers

Have a look at basename()

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

Also, dirname() fetches the other part

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

If you need even more information - there is 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'] gives you the file without the extension.

like image 108
Emil Ivanov Avatar answered Sep 18 '22 14:09

Emil Ivanov


echo basename($string);

Take a look at the basename function.

like image 23
erenon Avatar answered Sep 21 '22 14:09

erenon