2

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
  • 1
    Comment out $uploaderFactory->addValidateCallback('custom_image_upload', $imageAdapter,'validateUploadFile'); Commented Apr 30, 2018 at 12:07

3 Answers 3

2

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
}
-1

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.

1
  • are u sure !!!! Commented Jun 14, 2020 at 5:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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