Laravel Get File Mime Type from Storage Example

By Hardik Savani November 5, 2023 Category : Laravel

Hello Folks,

This article goes in detailed on laravel storage get mime type. This article goes in detailed on laravel get mime type from url. let’s discuss about how to get mime type of file in laravel. This article goes in detailed on how to get mime type in laravel.

Laravel offers the Storage and File facades for managing file systems. In this guide, I will demonstrate how to retrieve file mime type from both the storage and public folders within Laravel. You can refer to the following examples to learn how to access file content.

Review the following one by one example:

Example 1: Laravel Get File Mime Type using Storage

here is a controller code of getting file mime type from Storage facade.

app/Http/Controllers/FileController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\Storage;

class FileController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

$mimeType = Storage::mimeType('upload/test.txt');

dd($mimeType);

}

}

Output:

text/plain

Example 2: Laravel Get File Mime Type using using File

here is a controller code of getting file mime type from File facade.

app/Http/Controllers/FileController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use File;

class FileController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

$mimeType = File::mimeType('upload/test.txt');

dd($mimeType);

}

}

Output:

text/plain

I hope it can help you...

Tags :
Shares