Do an if for another line

Asked

Viewed 62 times

0

I have this e-mail 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 an error, I believe it is on account of the 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, would I 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('*/*/');
    }
}
  • 2

    "it gives an error", "I get this error", which error?

  • Do you want to go over the bugs? Just take out everything that has Exit;

1 answer

1


If you’re not sending files, you don’t need to process the superglobal validation $_FILES.

Check if it’s 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
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.