phplaravelmaatwebsite-excellaravel-excellaravel-herd

File upload error - unable to create a temporary file in Herd/Laravel


I'm using the following stack:

What I'm Trying to Do

I want to send a POST request containing an Excel file (.xlsx). This file should then be processed using the maatwebsite/excel package to create new records based on its contents.

The Problem

When I send the POST request using Postman with form-data containing the Excel file, I get this error:

Warning: File upload error - unable to create a temporary file in Unknown on line 0

Here’s a screenshot for more details: error screnshoot

Here's my code

routes\api.php

Route::controller(AlumniController::class)->group(function () {
    ...
    Route::post('/alumnis/import', 'importExcel'); // this is the endpoint for import excel
});

app\Http\Controllers\AlumniController.php

use Illuminate\Http\Request;
use App\Imports\AlumnisImport;
use Maatwebsite\Excel\Facades\Excel;

class AlumniController extends Controller
{
    ...

    public function importExcel()
    {
        try {
            Excel::import(new AlumnisImport, request()->file('alumni_excel'));
        } catch (\Throwable $th) {
            dd($th);
        }
    }
}
app\Imports\AlumnisImport.php

use App\Models\Alumni;
use App\Models\Jurusan;
use Maatwebsite\Excel\Concerns\ToModel;

class AlumnisImport implements ToModel
{
    /**
     * @param array $row
     *
     * @return \Illuminate\Database\Eloquent\Model|null
     */
    public function model(array $row)
    {
        $jurusan = Jurusan::firstOrCreate(
            ['nama' => $row['nama_jurusan']],
            ['nama' => $row['nama_jurusan']]
        );

        return new Alumni([
            'nama' => $row['nama'],
            'tgl_lahir' => $row['tgl_lahir'],
            'tahun_mulai' => $row['tahun_mulai'],
            'tahun_lulus' => $row['tahun_lulus'],
            'no_tlp' => $row['no_tlp'],
            'email' => $row['email'],
            'password' => isset($row['password']) ? $row['password'] : null,
            'alamat' => $row['alamat'],
            'tempat_kerja' => $row['tempat_kerja'] ?? null,
            'jabatan_kerja' => $row['jabatan_kerja'] ?? null,
            'tempat_kuliah' => $row['tempat_kuliah'] ?? null,
            'prodi_kuliah' => $row['prodi_kuliah'] ?? null,
            'kesesuaian_kerja' => isset($row['kesesuaian_kerja']) ? filter_var($row['kesesuaian_kerja'], FILTER_VALIDATE_BOOLEAN) : null,
            'kesesuaian_kuliah' => isset($row['kesesuaian_kuliah']) ? filter_var($row['kesesuaian_kuliah'], FILTER_VALIDATE_BOOLEAN) : null,
            'photo' => $row['photo'] ?? null,
            'jurusan_id' => $jurusan->id,
        ]);
    }
}

Questions

Could this issue be related to Laravel Herd? What other things could I look into?


Solution

  • UPDATE

    I've found the solution.

    1. open laravel herd
    2. go to PHP menu
    3. right click on php version
    4. select "open php.ini directory"
    5. edit your php.ini
    6. uncomment and set your sys_temp_dir
    // before
    ;sys_temp_dir = "/tmp"
    
    // after
    sys_temp_dir = "C:\Users\<YOUR_USERNAME>\AppData\Local\Temp"
    

    Different operating system may have different temp folder paths too.