lacksinho's avatar

Laravel File Upload error

Hi, I get this error laravel file upload. Any idea how to solve this? (see the error below)

TypeError Illuminate\Http\UploadedFile::createFromBase(): Argument #1 ($file) must be of type Symfony\Component\HttpFoundation\File\UploadedFile, int given, called in /var/www/html/vendor/laravel/framework/src/Illuminate/Http/Concerns/InteractsWithInput.php on line 426

0 likes
7 replies
lacksinho's avatar

@Sinnbeck

//Post method

public function postApplicantsImport(ImportApplicantRequest $request)
{
    AuthService::hasAccess($this->rootMenu, 'import_applicants');
    try {
        Excel::import(new ApplicantsImport($request), $request->file('excel_file'));
        $message = "Applicant Import completed successfully.";
        $alertType = 'success';
        $notification = GeneralService::setNotification($message, $alertType);
        return redirect()->back()->with($notification);
    } catch (\Maatwebsite\Excel\Validators\ValidationException $e) {
        $failures = $e->failures();
        foreach ($failures as $failure) {
            $form4Index = $failure->values()['form4_index'];
            $sn = $failure->values()['sn'];
            $errors = implode(',', $failure->errors());
            $logInput = [
                'sn' => $sn,
                'form4_index' => $form4Index,
                'attribute' => $failure->attribute(),
                'errors' => $errors,
            ];

            ImportApplicantLog::create($logInput);
        }

        $message = "Applicant Import failed.";
        $alertType = 'error';
        $notification = GeneralService::setNotification($message, $alertType);
        return redirect()->route('admin.import_applicant_logs')->with($notification);
    }
}

// ImportApplicantRequest

namespace App\Http\Requests;

use App\Rules\FileExtensionRule; use Illuminate\Foundation\Http\FormRequest;

class ImportApplicantRequest extends FormRequest { protected $arrayExtension = ['xlsx', 'xls', 'csv', 'ods'];

/**
 * Determine if the user is authorized to make this request.
 *
 * @return bool
 */
public function authorize()
{
    return true;
}

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'app_round' => 'required',
        'campus' => 'required',
        'authority' => 'required',
        'excel_file' => ['required', 'max:1024', new FileExtensionRule($this->arrayExtension)]
    ];
}

}

Sinnbeck's avatar

Can I suggest that you add the file rule

'excel_file' => ['required', 'file', 'max:1024', new FileExtensionRule($this->arrayExtension)] 
lacksinho's avatar

@Sinnbeck

Again still the same error

/var/www/html/vendor/laravel/framework/src/Illuminate/Http/UploadedFile.php }

/**
 * Get the file's extension supplied by the client.
 *
 * @return string
 */
public function clientExtension()
{
    return $this->guessClientExtension();
}

/**
 * Create a new file instance from a base instance.
 *
 * @param  \Symfony\Component\HttpFoundation\File\UploadedFile  $file
 * @param  bool  $test
 * @return static
 */
public static function createFromBase(SymfonyUploadedFile $file, $test = false)
{
    return $file instanceof static ? $file : new static(
        $file->getPathname(),
        $file->getClientOriginalName(),
        $file->getClientMimeType(),
        $file->getError(),
        $test
    );
}

/**
 * Parse and format the given options.
 *
 * @param  array|string  $options
 * @return array
 */
protected function parseOptions($options)
{
    if (is_string($options)) {
        $options = ['disk' => $options];

Arguments "Illuminate\Http\UploadedFile::createFromBase(): Argument #1 ($file) must be of type Symfony\Component\HttpFoundation\File\UploadedFile, int given, called in /var/www/html/vendor/laravel/framework/src/Illuminate/Http/Concerns/InteractsWithInput.php on line 426

Sinnbeck's avatar

@lacksinho check the stack trace in the error to see where in your code the error originates. Or share the error page

Please or to participate in this conversation.