0

I'm currently working on a field that adds several image files in the same field by saving it with json_encode.

The addition works well, I display my images in Front and Back.

My concern is that when I check the checkbox to delete the images of a post , it gives me the error.

enter image description here

Blockquote

There has been an error processing your request File was not uploaded.

Trace:
#0 /vagrant/httpdocs/app/code/community/AW/Blog/controllers/Manage/BlogController.php(137): Varien_File_Uploader->__construct(Array)
#1 /vagrant/httpdocs/app/code/core/Mage/Core/Controller/Varien/Action.php(418): AW_Blog_Manage_BlogController->saveAction()
#2 /vagrant/httpdocs/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(250): Mage_Core_Controller_Varien_Action->dispatch('save')
#3 /vagrant/httpdocs/app/code/core/Mage/Core/Controller/Varien/Front.php(172): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#4 /vagrant/httpdocs/app/code/core/Mage/Core/Model/App.php(354): Mage_Core_Controller_Varien_Front->dispatch()
#5 /vagrant/httpdocs/app/Mage.php(684): Mage_Core_Model_App->run(Array)
#6 /vagrant/httpdocs/index.php(74): Mage::run('', 'store')
#7 {main}

Blockquote

My code is :

Controller

public function saveAction()
    {
         if ($data = $this->getRequest()->getPost()) {
            $model = Mage::getModel('blog/post');
            if ($this->getRequest()->getParam('id')) {
                $model->load($this->getRequest()->getParam('id'));
            }

            //add multiple image

            if(isset($_FILES['carouselblog1']['name']) && !empty($_FILES['carouselblog1']['name'])) {

                $uploadedPicture = [];

                foreach ($_FILES['carouselblog1']['name'] as $groupKey => $groupfile) {
                    foreach ($_FILES['carouselblog1']['name'][$groupKey] as $key => $currentfile) {
                        $uploader = new Varien_File_Uploader(
                            array(
                                'name' => $_FILES['carouselblog1']['name'][$groupKey][$key],
                                'type' => $_FILES['carouselblog1']['type'][$groupKey][$key],
                                'tmp_name' => $_FILES['carouselblog1']['tmp_name'][$groupKey][$key],
                                'error' => $_FILES['carouselblog1']['error'][$groupKey][$key],
                                'size' => $_FILES['carouselblog1']['size'][$groupKey][$key]
                            )
                        );

                        // Any extention would work
                        $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
                        $uploader->setAllowRenameFiles(false);
                        $uploader->setFilesDispersion(false);
                        // We set media as the upload dir
                        $path = Mage::getBaseDir('media') . DS .'blog'.DS. $model->getId();
                        $uploader->save($path, $_FILES['carouselblog1']['name'][$groupKey][$key]);

                        $uploadedPicture['blog'.DS.$model->getId().DS.$_FILES['carouselblog1']['name'][$groupKey][$key]] =
                                isset($_POST['carouselblogTitle'][$groupKey][$key])
                                    ? $_POST['carouselblogTitle'][$groupKey][$key] : '' ;
                    }
                }
                //this way the name is saved in DB
                $data['carouselblog1'] = json_encode($uploadedPicture/*JSON_PRETTY_PRINT*/);

            }
           //  handle delete image
            else {
                if(isset($data['carouselblog1']['delete']) && $data['carouselblog1']['delete'] == 1)
                    $data['carouselblog1'] = '';
                   // $imageName = '';
                else
                    unset($data['carouselblog1']);

            }

image.php

    protected function _getDeleteCheckbox()
    {
        $html = '';
        if ($this->getValue()/*&& $this->getHtmlId()=='featured_image'*/) {
            $label = Mage::helper('core')->__('Delete Image');
            $html .= '<span class="delete-image">';
            $html .= '<input type="checkbox"'
                . ' name="' . parent::getName() . '[delete]" value="1" class="checkbox"'
                . ' id="' . $this->getHtmlId() . '_delete"' . ($this->getDisabled() ? ' disabled="disabled"': '')
                . '/>';
            $html .= '<label for="' . $this->getHtmlId() . '_delete"'
                . ($this->getDisabled() ? ' class="disabled"' : '') . '> ' . $label . '</label>';
            $html .= $this->_getHiddenInput();
            $html .= '</span>';
        }
//var_dump(parent::getName(),$this->getValue());

        return $html;
    }

    /**
     * Return html code of hidden element
     *
     * @return string
     */
    protected function _getHiddenInput()
    {
            return '<input type="hidden" name="' . parent::getName() . '" value="' . $this->getValue() . '" />';
    }

I need to delete all content of table when i check the checkbox!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.