ItSolutionStuff.com

How to Get Filename from File Path in MySQL Query?

By Hardik Savani • November 5, 2023
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 :

categories table:

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().

MySQL Query:

SELECT id, name, SUBSTRING_INDEX(image, '/', -1) FROM `categories`

After this, you will see output like as bellow screen shot:

Output:

I hope it can help you....

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Create a REST API using Node.js and MySQL?

Read Now →

PHP MySQL Image Gallery CRUD Example

Read Now →

PHP MySQL Highcharts Chart Example

Read Now →

PHP Import Excel File into MySQL Database Tutorial

Read Now →

MySQL Query to Remove Special Characters from String

Read Now →

How to Add MySQL Trigger from Migration in Laravel?

Read Now →

How to Import CSV File using MySQL?

Read Now →

How to fetch this week records in MySql ?

Read Now →

How to Check Empty or Null Data using MySQL Query?

Read Now →

How to Copy One Table Data into Another Table using MySQL?

Read Now →

How to count unique domains from email address field in MySQL ?

Read Now →

Mysql procedure with pagination in laravel?

Read Now →

Which MySQL Datatype use for Store an IP Address?

Read Now →

Mysql Hexadecimal Color Code Store in Binary Datatype Example

Read Now →