How to get filename from file path in MySQL?By Hardik Savani | November 30, 2016 | | 3429 Viewer | Category : PHP MySql
Sometimes we require to get filenames from path of table column in MySQL. If you are working on PHP then you can simply use basename() pre-define function of code PHP, But in MySQL you can't use basename() in select query. This mysql function you can use in code PHP, PHP framework like laravel, codeigniter etc.
However, you can get simply filename or image name from path using SUBSTRING_INDEX() of MySQL. You can extract filename from path using substring_index(), substring_index() take three argument, first for column name, second for split string chat, and third get last element from split. So you can simply use. I am going to give you simply example for getting filenames from path or url.
In this example i have "categories" table with six columns i also added some dummy data on table as listed bellow :
Ok, now i have image column with full path of image, we require only get name of image not path so run bellow mysql query using substring_index().
SELECT id, name, SUBSTRING_INDEX(image, '/', -1) FROM `categories`
After this, you will see output like as bellow screen shot:
I hope it can help you....
- How to check file size when select multiple file validation using Jquery?
- How to convert Object into Array in PHP?
- How to add Delete cascade to existing column in Laravel 5, without remove column?
- How to get days difference between two dates in laravel?
- Laravel 5 - Barcode generator example using milon/barcode package
- Magento 2 - How to solve one or more indexers are invalid by command line?
- How to check word exist in string or not in PHP?
- Laravel 5 - Create Dummy Data using tinker factory