Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Rretzko's avatar
Level 15

Livewire validate fails on mime types with correct file

Hi - I'm trying to validate that an uploaded file is csv mime type on Livewire 3. When I upload a csv file, the validation fails. Any guidance is appreciated! Here's the livewire component:

<?php

namespace App\Livewire\Teachers\Schools;
use Livewire\Component;
use Livewire\WithFileUploads;
class SchoolEnsembleStudentComponent extends Component
{

    use WithFileUploads;
 	public $csv; //file upload

 	public function studentImport()
    	{
        	$validated = $this->validate(
            	[
                	'csv' => 'required|file|mimes:csv,txt,octet-stream|max:2048' //also tried application/octet-stream
            	]
        	);
	}
}

When I dd($this-csv) I get

Livewire\Features\SupportFileUploads\TemporaryUploadedFile {#1585 ▼ // app\Livewire\Teachers\Schools\SchoolEnsembleStudentComponent.php:272
  -test: false
  -originalName: "2BZoDc7jLBr0BZQqUruYn5hLPbPRiU-metabWFzc1VwbG9hZEVuc2VtYmxlU3R1ZGVudHNDb25jZXJ0Q2hvaXJfMTk4NF9zbWFsbEJhdGNoLmNzdg==-.txt"
  -mimeType: "application/octet-stream"
  -error: 0
  #hashName: null
  #disk: "local"
  #storage: 
Illuminate\Filesystem
\
FilesystemAdapter {#1230 ▶}
  #path: "livewire-tmp/2BZoDc7jLBr0BZQqUruYn5hLPbPRiU-metabWFzc1VwbG9hZEVuc2VtYmxlU3R1ZGVudHNDb25jZXJ0Q2hvaXJfMTk4NF9zbWFsbEJhdGNoLmNzdg==-.txt"
  path: "C:\xampp\htdocs\dev\tdr2025\storage\app\livewire-tmp"
  filename: "2BZoDc7jLBr0BZQqUruYn5hLPbPRiU-metabWFzc1VwbG9hZEVuc2VtYmxlU3R1ZGVudHNDb25jZXJ0Q2hvaXJfMTk4NF9zbWFsbEJhdGNoLmNzdg==-.txt"
  basename: "php5177.tmp"
  pathname: "C:\Users\rick\AppData\Local\Temp\php5177.tmp"
  extension: "tmp"
  realPath: "
C:\xampp\htdocs\dev\tdr2025\storage\app\livewire-tmp/2BZoDc7jLBr0BZQqUruYn5hLPbPRiU-metabWFzc1VwbG9hZEVuc2VtYmxlU3R1ZGVudHNDb25jZXJ0Q2hvaXJfMTk4NF9zbWFsbEJhdGNo
 ▶
"
  size: 89
  writable: false
  readable: false
  executable: false
  file: false
  dir: false
  link: false
}

Thanks for your thoughts!

0 likes
5 replies
LaryAI's avatar
Level 58

It looks like the issue you're encountering is related to the MIME type validation of the uploaded file. When Livewire handles file uploads, it creates a temporary file with a .tmp extension, which can sometimes cause MIME type validation to fail because the MIME type may not match what is expected for a .csv file.

To resolve this issue, you can use the mimetypes:text/csv validation rule instead of mimes:csv,txt,octet-stream. The mimetypes rule allows you to specify the exact MIME type of the file, which should be more reliable.

Here's how you can update your validation rule:

public function studentImport()
{
    $validated = $this->validate([
        'csv' => 'required|file|mimetypes:text/csv|max:2048'
    ]);

    // Handle the CSV file upload...
}

Additionally, ensure that your file input on the front end is correctly configured to accept CSV files. Here's an example of how your file input might look in your Livewire component's Blade template:

<input type="file" wire:model="csv" accept=".csv">

The accept attribute helps to guide users to select the correct file type, although it does not enforce it on the server side.

If you continue to have issues, you might want to check the actual MIME type of the file being uploaded. You can do this by examining the file in your studentImport method before validation:

public function studentImport()
{
    // Check the MIME type of the uploaded file
    $mimeType = $this->csv->getMimeType();
    \Log::info("Uploaded file MIME type: {$mimeType}");

    // Continue with validation and processing...
}

This will log the MIME type to your application log, which can help you debug the issue further. If the MIME type is not what you expect, you may need to adjust your validation rule to accommodate the MIME type that your files are actually being uploaded with.

Rretzko's avatar
Level 15

Hi all - I rechecked and updated per suggestions and now getting NULL on the file update and generating error: "Call to a member function getMimeType() on null". Just to provide current info: Blade component

<form wire:submit="studentImport" enctype="multipart/form-data">

        <div>
            <label for="">Find Csv File</label>
            <input type="file" wire:model="csv" accept=".csv" name="csv" > 
        </div>

        @error('csv') 
			<div>{{ $message }}</div> 
		@enderror

        <input type="submit" value="Upload Ensemble Students"/>

    </form>

Livewire 3 component

<?php

namespace App\Livewire\Teachers\Schools;
use Livewire\Component;
use Livewire\WithFileUploads;
class SchoolEnsembleStudentComponent extends Component
{

    use WithFileUploads;
 	public $csv; //file upload

 	public function studentImport()
    	{

		$mimeType = $this->csv->getMimeType();
//Generates error: Call to a member function getMimeType() on null
        Log::info("Uploaded file MIME type: ($mimeType)");
	}
}

Thanks!

Rretzko's avatar
Level 15

I reimplemented the form to bare metal and now generating the mimeType of text/plain.

<form wire:submit="studentImport" enctype="multipart/form-data" >

        <input type="file" wire:model="csv" name="csv" />
        <input type="submit" value="Submit" />
    </form>

With this, both csv and jpg files pass validation.

$validated = $this->validate(
            [
                'csv' => 'required|file|mimeTypes:text/plain|max:2048'
            ]
        );
        dd($validated);

and trying to get the mime type generates the previous NULL error.

				$mimeType = $this->csv->getMimeType();
				Log::info('mimeType: ' . $mimeType);
~~
I have no doubt that I'm doing something wrong, but I can't see it.  Thanks for any help you can offer!
Rretzko's avatar
Level 15

@Snapey Incredible! My test file had a header row and a data row. I moved to a larger test file and it worked. I'm listening to the episode now. Thank you....again....!

Please or to participate in this conversation.