1

I am getting this error when I submit the form from the front end. Although I set the required extension in the setAllowedExtensions

Exception #0 (InvalidArgumentException): Disallowed file type.

if($files!='')
    {
        foreach ($files as $key => $value) {

            $uploaderFactory = $this->uploaderFactory->create(['fileId' => $key]);
            $uploaderFactory->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png','pdf', 'docx', 'doc']);
            $imageAdapter = $this->adapterFactory->create();
            /* start of validated image */
            $uploaderFactory->addValidateCallback('custom_image_upload',
                $imageAdapter,'validateUploadFile');
            $uploaderFactory->setAllowRenameFiles(true);
            $uploaderFactory->setFilesDispersion(true);
            $mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
            $destinationPath = $mediaDirectory->getAbsolutePath('custom_module');
            $result = $uploaderFactory->save($destinationPath);

            $imagepath = $result['file'];
            //print_r($imagepath);exit;

            $saveData['custom_file'] = $imagepath;
            //$this->model->setData($saveData);

            //$submitModel->setData($saveData);
            //$submitModel->save();
        }
    }

When I upload a jpeg or png it works fine ,but gives error on doc,docx and pdf

  • 1
    Comment out $uploaderFactory->addValidateCallback('custom_image_upload', $imageAdapter,'validateUploadFile'); – Vivek Kumar Apr 30 '18 at 12:07
1

The problem with following code

$uploaderFactory->addValidateCallback('custom_image_upload',
                $imageAdapter,'validateUploadFile');

You are using the Image Adapter factory that will not allow you upload the files. You can use File Uploader instead.

Magento\Framework\File\Uploader
0

You have to add this function to your class

private function validateUploadFile($filePath)
{
     return;
    // @todo
    // your custom validation code here
}
0

Please try below code

$uploaderFactory->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png','pdf', 'docx', 'doc'));

instead of

$uploaderFactory->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png','pdf', 'docx', 'doc']);

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.