Make an if for another line

0

I have this email sending code and the attachment, it is working perfectly when I send the email along with the attachment, but if I do not send attachment it gives an error, I believe it is on account of line:

if(filter_input(INPUT_POST, "btnSubmit")){
    if ($_FILES['arquivo']['error'] != 0) {
        echo $this->__("Não foi possível fazer o upload, erro:" . $_UP['erros'][$_FILES['arquivo']['error']]);
        exit; 
    }
}

I get this error when sending without image, I would like to put it to go to the other line that starts at $post = $this->getRequest()->getPost();

public function postAction()
{
    $_UP['pasta'] = '/opt/lampp/htdocs/magento/media/teste/';
    //
    $_UP['caminho'] = '/media/teste/';

    $_UP['tamanho'] = 1024 * 1024 * 2; // 2Mb

    $_UP['extensoes'] = array('jpg', 'png', 'gif', 'pdf', 'txt', 'doc', 'docx');

    $_UP['renomeia'] = false;

    if(filter_input(INPUT_POST, "btnSubmit")){
        if ($_FILES['arquivo']['error'] != 0) {
            echo $this->__("Não foi possível fazer o upload, erro:" . $_UP['erros'][$_FILES['arquivo']['error']]);
            exit; 
        }
        $extensao = strtolower(end(explode('.', $_FILES['arquivo']['name'])));
        if (array_search($extensao, $_UP['extensoes']) === false) {
            exit;
        }
        if ($_UP['tamanho'] < $_FILES['arquivo']['size']) {
            exit;
        }
        if ($_UP['renomeia'] == true) {
            $nome_final = md5(time()).'.jpg';
        } else {
            $nome_final = $_FILES['arquivo']['name'];
        }
        if (move_uploaded_file($_FILES['arquivo']['tmp_name'], $_UP['pasta'] . $nome_final)) {
            $connection = Mage::getSingleton('core/resource')->getConnection('core_write');
            $query = "INSERT INTO contato ('nome','caminho') VALUES ('".$nome_final."', '".$_UP['pasta']."')";
            $connection->query($query);
        } else {
        }
    }

    $post = $this->getRequest()->getPost();
    if ( $post ) {
        $translate = Mage::getSingleton('core/translate');
        /* @var $translate Mage_Core_Model_Translate */
        $translate->setTranslateInline(false);
        try {
            $postObject = new Varien_Object();
            $postObject->setData($post);

            $error = false;

            if (!Zend_Validate::is(trim($post['name']) , 'NotEmpty')) {
                $error = true;
            }

            if (!Zend_Validate::is(trim($post['comment']) , 'NotEmpty')) {
                $error = true;
            }

            if (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
                $error = true;
            }

            if (Zend_Validate::is(trim($post['hideit']), 'NotEmpty')) {
                $error = true;
            }

            if ($error) {
                throw new Exception();
            }
            $mailTemplate = Mage::getModel('core/email_template');
            /* @var $mailTemplate Mage_Core_Model_Email_Template */
            $mailTemplate->setDesignConfig(array('area' => 'frontend'))
                ->setReplyTo($post['email'])
                ->addBcc($post['email'])
                ->sendTransactional(
                    Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE),
                    Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
                    Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
                    null,
                    array('data' => $postObject)
                );

            if (!$mailTemplate->getSentSuccess()) {
                throw new Exception();
            }


            $translate->setTranslateInline(true);

            Mage::getSingleton('customer/session')->addSuccess(Mage::helper('contacts')->__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.'));
            $this->_redirect('*/*/');

            return;
        } catch (Exception $e) {
            $translate->setTranslateInline(true);

            Mage::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('Unable to submit your request. Please, try again later'));
            $this->_redirect('*/*/');
            return;
        }

    } else {
        $this->_redirect('*/*/');
    }
}
    
asked by anonymous 17.08.2017 / 22:37

1 answer

1

If you are not uploading files, you do not need to process the validation of the superglobal $_FILES .

Check if it is empty first with empty() :

if(filter_input(INPUT_POST, "btnSubmit")){
    if(!empty($_FILES)) { // só processar se $_FILES tiver conteúdo
        if ($_FILES['arquivo']['error'] != 0) {
            echo $this->__("Não foi possível fazer o upload, erro:" . $_UP['erros'][$_FILES['arquivo']['error']]);
            exit; 
        }
    //etc
    } // feche o if logo antes do último
}
    
18.08.2017 / 01:20