0

I have a frontend form which consists of two fields. One is a PDF file uploader field. The second one is a ZIP file uploader field.

Now how do I save these two fields in the database? In which directory do I need to save the PDF files and ZIP files?

0

See my Controller for upload pdf.

<?php
namespace <Vendor>\<ModuleName>\Controller\Adminhtml\brochures;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Backend\App\Action;
/**
 * Description of Upload
 *
 * @author dharmendra
 */
class Upload extends \Magento\Backend\App\Action
{
    protected $_fileUploaderFactory;
    protected $_directory_list;
    protected $_filesystem;
    protected $_storeManager; 

    public function __construct(
        Action\Context $context,
        \Magento\Framework\Registry $coreRegistry,
        \Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory,
        \Magento\Framework\App\Filesystem\DirectoryList $directory_list,
        \Magento\Framework\Filesystem $filesystem,
        \Magento\Store\Model\StoreManagerInterface $storeManager
    ) {
        $this->_fileUploaderFactory = $fileUploaderFactory;
        $this->_directory_list = $directory_list;
        $this->_storeManager = $storeManager;
        $this->_filesystem = $filesystem;
        parent::__construct($context, $coreRegistry);
    }

    public function execute(){
        $success = false;
        $extError = true;
        $responseArray = array();

        $filenameArray = isset($_FILES['imagename']['name']) ? $_FILES['imagename']['name'] : array();
        $filename = "";
        foreach ($filenameArray as $index => $value) {
            $ext = pathinfo($value['file'], PATHINFO_EXTENSION);
            $filename = $value['file'];
        }

        if(in_array(strtolower($ext), ['pdf', 'doc', 'docx'])) {

            if($filename != ''){
                $path = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath('productpdf');

                $renamedFileName = strtotime('now').'_'.str_replace(' ','_',$filename);
                $targetFile = $path.'/'. $renamedFileName;

                $tmpnameArray = isset($_FILES['imagename']['tmp_name']) ? $_FILES['imagename']['tmp_name'] : array();
                $tempname = "";

                foreach ($tmpnameArray as $index => $value) {
                    $tempname = $value['file'];
                }

                if($tempname != ''){

                    if (move_uploaded_file($tempname, $targetFile)) {
                        $success = true;
                    }
                }
            }
        } else {
            $extError = false;
            $message = __('Please upload only PDF, DOC, DOCX.');
        }

if($success){

            $mediaUrl = $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);    
            $data['imagename'] = $renamedFileName;
        }
$model->setData($data);

        try {
            $model->save();

            $this->messageManager->addSuccess(__('The Brochures has been saved.'));
            $this->_objectManager->get('Magento\Backend\Model\Session')->setFormData(false);
            if ($this->getRequest()->getParam('back')) {
                return $resultRedirect->setPath('*/*/edit', ['id' => $model->getId(), '_current' => true]);
            }
            return $resultRedirect->setPath('*/*/');
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            $this->messageManager->addError($e->getMessage());
        } catch (\RuntimeException $e) {
            $this->messageManager->addError($e->getMessage());
        } catch (\Exception $e) {
            echo $e->getMessage();die;
            $this->messageManager->addException($e, __('Something went wrong while saving the Brochures.'));
        }
    }
}

Still you facing any issue let me know.

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.