Get filename from url, How to get file name from full path/url with PHP, filename and directory path, get filename from path php, get filename from url php, Get only filename from url.
How to split filename and directory path from URL in php? Answer is simple but today i got confused for fetching filename and parent directory’s path from URL in php.
I trying fetching filename and dirname by explode(), str_replace() etc., function but it create more complexity in code.
After more searching and R&D i found basename() and dirname() function in php for getting filename and parent directory’s path from URL. It works like charm. Here is example use of both functions.
Use of basename and dirname in php:
basename ? Returns trailing name component of path
dirname ? Returns parent directory’s path
Code for filename and directory path from url
//file url source $fileSrc = 'http://mysite/assets/image/my-image.jpg'; echo $filename = basename($fileSrc); echo '<br/>'; echo $dirpath = dirname($fileSrc);
Here is result: