How to Access TemporaryUploadedFile Data from FileUpload

Hi!

I’m using the FileUpload component in my Livewire form. I want to access information about the uploaded file, such as its size, width, and height, using the TemporaryUploadedFile. Can someone explain how I can achieve this?

FileUpload::make('image')
          ->label('Case Image')
          ->image() 
          ->imageEditor()
          ->openable()
          ->downloadable()
          ->previewable()
          ->required() 
          ->disk('s3') 
          ->directory('media/cases')
          ->maxSize(256)
          ->placeholder('Drag and drop or browse to upload your image')
          ->reactive()
          ->afterStateUpdated(function (?string $state, Set $set, Get $get, TemporaryUploadedFile $file) {
              dd($file->getSize(), $file->dimensions());
          }),


I can retrieve the necessary information using getUploadedFileNameForStorageUsing, but I cannot do so with afterStateUpdated. My goal is to use afterStateUpdated to display the data in real time, whereas getUploadedFileNameForStorageUsing sets the data only after the changes have been saved.

->getUploadedFileNameForStorageUsing(function (Set $set, Get $get, TemporaryUploadedFile $file){
    $fileSize = $file->getSize(); 
    $fileDimensions = $file->dimensions();

    $set('size', round($fileSize / 1024, 2));
    $set('width', $fileDimensions[0]);
    $set('height', $fileDimensions[1]);
    
    $hash = str()->random(30);
    $timestamp = now()->timestamp;  
    $extension = '.' . $file->getClientOriginalExtension();

    return $hash.$timestamp.$extension;
}),  
Solution
try TemporaryUploadedFile $state
Was this page helpful?