public/images/portfolio/i-vis/1.jpg
How could i remove all the path regardless of what the filename is using 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.
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.
The pathinfo() function returns information about a file path.
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.
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.
echo basename($string);
Take a look at the basename function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With