get file name without extension in laravel?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Title: Get Filename Without Extension in Laravel: A Comprehensive Guide

Introduction: In Laravel, there might be times when you need to access the filename of an uploaded file without its extension. This comes in handy for various reasons, such as storing the information separately or organizing files more effectively. While the standard approach is using Input::file('upfile')->getClientOriginalName(), this returns a name with the extension attached. In this blog post, we will shed light on alternative methods that allow you to get the filename without the extension in Laravel.

  1. Using String Manipulation
    One of the most straightforward ways is to use string manipulation functions to achieve your goal. Here's an example code snippet:
php
$filename = 'qwe.jpg'; // File name with extension
$file_name_without_extension = str_replace('.', '', $filename);

In this example, we first declare a file name with its extension (qwe.jpg), and then use the str_replace() function to remove the '.' character and everything that follows it from the string. The output will be "qwe" which is the filename without the extension.

  1. Using Laravel FileSystem Facade Methods
    Another approach is using the Laravel FileSystem facade methods, specifically basename() and getExtension(). Here's an example code snippet:
php
$filename = 'qwe.jpg'; // File name with extension
$file_name_without_extension = basename(str_replace('.' . getExtension($filename), '', $filename));

In this example, we first declare the filename (qwe.jpg) and use basename() to extract just the base part of the file name. Next, we call getExtension() on the same filename to retrieve its extension (jpg). Finally, we combine these two results to get the filename without the extension, "qwe", by using str_replace() and replacing the extension part with an empty string.

  1. Using Laravel's Request Validation Rules
    Another way to tackle this problem is by defining a custom validation rule in your controller or form request. Define a rule like 'file_name_no_ext' => 'required|regex:/^(?:[^.]+)(?=\.[^.]*$)/', which matches file names with no extension at the end.
    Here's an example code snippet:
php
$rule = ['file_name_no_ext' => 'required|regex:/^(?:[^.]+)(?=\.[^.]*)$/'];
$filename = $request->input('upfile')->getClientOriginalName(); // File name with extension
validationRule($rule, $filename);

In this example, we first define the custom validation rule and then call it in our code, passing the original filename (qwe.jpg) as input. The output of this method will return the filename without the extension, i.e., "qwe".

  1. Custom Helper Function
    Finally, if you want a cleaner solution, consider using a custom helper function that can be easily invoked across your application. Here's an example code snippet to define such a function:
php
function getFileNameWithoutExtension($filename) {
  return str_replace('.', '', $filename);
}

$filename = 'qwe.jpg'; // File name with extension
$file_name_without_extension = getFileNameWithoutExtension($filename);

In this example, we create the function getFileNameWithoutExtension(), which takes a filename ($filename) and returns its base part without the extension. We then call this function on our file name (qwe.jpg) to get the desired output.

Conclusion: In conclusion, there are various methods available in Laravel to retrieve just the filename without the extension. From using simple string manipulation functions to leveraging custom validation rules or defining your own helper functions, you can find a solution that best fits your use case. Just remember to always ensure your code is structured and readable for other developers who might need to work with it in the future.