0

This is my code,I have create upload file in magento2 now i want to add upload file size limit. this is my code.

$uploader = $this->_objectManager->create('Magento\MediaStorage\Model\File\Uploader',['fileId' => "po_file"]);
    $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png', 'pdf', 'docs', 'doc', 'docx']);

    /** @var \Magento\Framework\Image\Adapter\AdapterInterface $imageAdapter */
    $imageAdapter = $this->_objectManager->get('Magento\Framework\Image\AdapterFactory')->create();

    // $uploader->addValidateCallback("po_file", $imageAdapter, 'validateUploadFile');
    $uploader->setAllowRenameFiles(true);
    $uploader->setFilesDispersion(true);

    /** @var \Magento\Framework\Filesystem\Directory\Read $mediaDirectory */
    $mediaDirectory = $this->_objectManager->get('Magento\Framework\Filesystem')->getDirectoryRead(DirectoryList::MEDIA);
    $result = $uploader->save($mediaDirectory->getAbsolutePath('admin/po_number'));
    $a = $result['file'];
    $this->_customerSession->setFilePath($a);

now any size image upload like 2 mb 3mb now i want restritc it to 2 mb

2
  • explain breifly Mar 27, 2019 at 5:35
  • see my question updated
    – imtiaz
    Mar 27, 2019 at 5:36

2 Answers 2

1

I'm not sure how you setup the upload but in PHP you can do stuff like this:

if(isset($_FILES['uploaded_file'])) {
    $maxsize    = 2097152;

    if(($_FILES['uploaded_file']['size'] >= $maxsize)) {
        exit('File too large. File must be less than 2 megabytes.');
    }
}

On top of that you can use php.ini settings to limit the upload size:

; Maximum allowed size for uploaded files.
upload_max_filesize = 40M

; Must be greater than or equal to upload_max_filesize
post_max_size = 40M
0

May be you need to update your php.ini like below:

post_max_size = 8M

upload_max_filesize = 8M

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.