elimariaaa
elimariaaa

Reputation: 866

Uploading PDF - file does not have a valid mime type in CodeIgniter 4

I am trying to upload a PDF file in my CI 4 web app but I keep getting file does not have a valid mime type.

I have no issues with uploading images, just PDF. I made sure I accept both in my validation.

On frontend, I use dropzone.js. I also declared that the accepted files are images and PDF.

autoProcessQueue: false,
uploadMultiple: false,
parallelUploads: 1,
maxFiles: 1,
acceptedFiles: "image/*,application/pdf",

And in my controller:

helper(['form', 'url']);
$validateImage = $this->validate([
 'file' => [
    'uploaded[file]',
    'mime_in[file, image/png, image/jpg, image/jpeg, image/gif, application/pdf]',
    'max_size[file, 2048000]',
  ],
]);

if (!$validateImage) {
  $imageFile = $this->request->getFile('file');
  echo \Config\Services::validation()->listErrors();
  echo $imageFile->getSize(); //128373
  echo $imageFile->getMimeType(); //application/pdf
  echo $imageFile->getClientMimeType(); //application/pdf
}

Here's my PHP Info:

The file is too light and the mime type is declared. Is there anything else I'm missing?

Upvotes: 1

Views: 2861

Answers (2)

Andre Rio
Andre Rio

Reputation: 13

Your form must contain this;

enctype="multipart/form-data"

Upvotes: 1

elimariaaa
elimariaaa

Reputation: 866

So... The space in the mime_in is the problem. I can't express how much my ignorance disappoints me. LOL.

From:

mime_in[file, image/png, image/jpg, image/jpeg, image/gif, application/pdf]',

To:

mime_in[file,image/png,image/jpg,image/jpeg,image/gif,application/pdf]',

Upvotes: 2

Related Questions