Fatal error: Can not unset string offsets

0

After uploading my site, the form to insert images no longer works, clicking the button to save I get:

  

Fatal error: Can not unset string offsets in ... app / Plugin / Uploader / Model / Behavior / FileValidationBehavior.php on line 283

Whether to select image or not. What is the solution to this problem?

I'm using cake version 2.4.4.

Model

<?php

App::uses('AppModel', 'Model');
class GalleryImage extends AppModel{

    public $displayField ='path';
    public $useTable = 'gallery_images';

    //public $actsAs = array('MultipleDisplayFields' => array('fields' => array('path', 'id')));

    var $name = 'GalleryImage';

    var $validate= array(
        'path' => array(
            'is_valid' => array(
                'rule' => 'notEmpty'/*'fileSelected'*/,
                'message' => 'Seleccione uma fotografia por favor.'//,
                //'allowEmpty' => false//,
                //'last' => true,
                //'required'=> true
                ),

            //'extension' => array(
            //  'rule' => array('extension', array('gif','jpeg','png','jpg')),
            //  'message'=> 'A imagem deve estar num formato gif, jpeg, png ou jpg.',
                //'last' => true,
                //'required'=> true
            //  ),
            'is_unique' => array(
                'rule' => 'isUnique',
                'message' => 'Uma fotografia com este nome já existe.'
                //'required'=> true
                )
            //),
        //'size' => array(
                //'rule' => array(/*'checkSize',true*/'fileSize','<=','2MB'),
                //'on' => 'create',
                //'message' => 'O ficheiro deve ter um tamanho igual ou inferior a 2MB.'

                //'last' => true,
                //'required'=> true
                )
        );
public $actsAs = array(
    'Uploader.FileValidation' => array(
        'file' => array(
            'extension' => array('gif', 'jpg', 'png', 'jpeg'),
            'type' => 'image',

            'filesize' => 2097152,
            'allowEmpty' => array(
                'value' => false,
                'error' => 'Seleccione uma fotografia por favor.'
                )
        )
    ),
    'Uploader.Attachment' => array(
        'file' => array(
                'finalPath' => '',
                'uploadDir' => 'img/gallery/',
                'dbColumn' => 'path'
            )
        )
    );
?>

Controller

public function admin_upload_image(){
        $this->set('title_for_layout', 'Inserir Fotografias');
        if(!$this->Session->check('User')) {
            $this->Session->setFlash('Está a aceder a uma zona restrita. Por favor faça Login.');
            $this->redirect(array(
                            'controller' => 'users',
                            'action' => 'login'));
        }

        $this->layout = 'admin_index';
        if($this->request->is('post') || $this->request->is('put')) {
          /*  $file = $this->request->data['gallery_images']['path']['name'];*/

        $file = array(
            'GalleryImage' => array(
                'path' => $this->request->data['GalleryImage']['file']['name']//,
                //'size' => $this->request->data['GalleryImage']['file']['size']
                   )
                );
            $this->loadModel('GalleryImage');

            $this->GalleryImage->create();

            if(/*$this->GalleryImage->save($file)*/$this->GalleryImage->save($this->request->data,true)){

               /*move_uploaded_file($this->data['GalleryImage']['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/html/PushUp/app/webroot/img/gallery/' . $this->data['GalleryImage']['file']['name']);
                */
                //$validationErrors = $this->GalleryImage->invalidFields();
                //$this->Session->setFlash($validationErrors['path']); // named key of the rule
                $this->Session->setFlash('Fotografia guardada com sucesso.', 'default', array('class'=>'alert alert-success'));
            }
            else{
                //$validationErrors = $this->GalleryImage->invalidFields();
               //$this->Session->setFlash($validationErrors['file']); // named key of the rule
               $this->Session->setFlash('Erro ao carregar o ficheiro!', 'default', array('class'=>'alert alert-danger'));
                //return false;

                //$error = $this->Notification->validationErrors;
                //$this->set('error', $error);

                //$this->Session->setFlash(__($error), 'Flash/warning');
            }
        }
    } 

FileValidationBehavior.php

<?php
/**
 * FileValidationBehavior
 *
 * A CakePHP Behavior that adds validation model rules to file uploading.
 *
 * @author      Miles Johnson - http://milesj.me
 * @copyright   Copyright 2006-2011, Miles Johnson, Inc.
 * @license     http://opensource.org/licenses/mit-license.php - Licensed under The MIT   License
 * @link        http://milesj.me/code/cakephp/uploader
 */

App::uses('ModelBehavior', 'Model');

App::import('Vendor', 'Uploader.Uploader');

class FileValidationBehavior extends ModelBehavior {

/**
 * Current settings.
 *
 * @access protected
 * @var array
 */
protected $_settings = array();

/**
 * Default list of validation sets.
 *
 * @access protected
 * @var array
 */

protected $_defaults = array(
    'width' => array(
        'rule' => array('width'),
        'message' => 'Your image width is invalid; required width is %s.'
    ),
    'height' => array(
        'rule' => array('height'),
        'message' => 'Your image height is invalid; required height is %s.'
    ),
    'minWidth' => array(
        'rule' => array('minWidth'),
        'message' => 'Your image width is too small; minimum width %s.'
    ),
    'minHeight' => array(
        'rule' => array('minHeight'),
        'message' => 'Your image height is too small; minimum height %s.'
    ),
    'maxWidth' => array(
        'rule' => array('maxWidth'),
        'message' => 'Your image width is too large; maximum width %s.'
    ),
    'maxHeight' => array(
        'rule' => array('maxHeight'),
        'message' => 'Your image height is too large; maximum height %s.'
    ),
    'filesize' => array(
        'rule' => array('filesize'),
        'message' => 'O ficheiro de ve ter um tamanho inferior a 2MB.'
    ),
    'extension' => array(
        'rule' => array('extension'),
        'message' => 'O ficheiro de ter um formato %s.'
    ),
    'required' => array(
        'rule' => array('required'),
        'message' => 'Seleccione uma fotografia por favor.',
        'on' => 'create',
        'allowEmpty' => true
    )
);

/**
 * Generated list of validation rules.
 *
 * @access protected
 * @var array
 */
protected $_validations = array();

/**
 * Setup the validation and model settings.
 *
 * @access public
 * @param Model $model
 * @param array $config
 * @return void
 */
public function setup(Model $model, $config = array()) {
    if (!empty($config)) {
        foreach ($config as $field => $options) {
            $this->_settings[$model->alias][$field] = $options + array('required' => true);
        }
    }
}

/**
 * Validates an image filesize. Default max size is 5 MB.
 *
 * @access public
 * @param Model $model
 * @param array $data
 * @param int $size
 * @return boolean
 */
public function filesize(Model $model, $data, $size = 5242880) {
    if (empty($size) || !is_numeric($size)) {
        $size = 5242880;
    }

    foreach ($data as $fieldName => $field) {
        if ($this->_allowEmpty($model, $fieldName, $field)) {
            return true;

        } else if (empty($field['tmp_name'])) {
            return false;
        }

        return ($field['size'] <= $size);
    }

    return true;
}

/**
 * Checks that the image height is exact.
 *
 * @access public
 * @param Model $model
 * @param array $data
 * @param int $size
 * @return boolean
 */
public function height(Model $model, $data, $size = 100) {
    return $this->_validateImage($model, $data, 'height', $size);
}

/**
 * Checks that the image width is exact.
 *
 * @access public
 * @param Model $model
 * @param array $data
 * @param int $size
 * @return boolean
 */
public function width(Model $model, $data, $size = 100) {
    return $this->_validateImage($model, $data, 'width', $size);
}

/**
 * Checks the maximum image height.
 *
 * @access public
 * @param Model $model
 * @param array $data
 * @param int $size
 * @return boolean
 */
public function maxHeight(Model $model, $data, $size = 100) {
    return $this->_validateImage($model, $data, 'maxHeight', $size);
}

/**
 * Checks the maximum image width.
 *
 * @access public
 * @param Model $model
 * @param array $data
 * @param int $size
 * @return boolean
 */
public function maxWidth(Model $model, $data, $size = 100) {
    return $this->_validateImage($model, $data, 'maxWidth', $size);
}

/**
 * Checks the minimum image height.
 *
 * @access public
 * @param Model $model
 * @param array $data
 * @param int $size
 * @return boolean
 */
public function minHeight(Model $model, $data, $size = 100) {
    return $this->_validateImage($model, $data, 'minHeight', $size);
}

/**
 * Checks the minimum image width.
 *
 * @access public
 * @param Model $model
 * @param array $data
 * @param int $size
 * @return boolean
 */
public function minWidth(Model $model, $data, $size = 100) {
    return $this->_validateImage($model, $data, 'minWidth', $size);
}

/**
 * Validates the ext and mimetype.
 *
 * @access public
 * @param Model $model
 * @param array $data
 * @param array $allowed
 * @return boolean
 */
public function extension(Model $model, $data, array $allowed = array()) {
    foreach ($data as $fieldName => $field) {
        if ($this->_allowEmpty($model, $fieldName, $field)) {
            return true;

        } else if (empty($field['tmp_name'])) {
            return false;

        } else {
            $ext = Uploader::ext($field['name']);
        }

        return (Uploader::checkMimeType($ext, $field['type']) && (empty($allowed) || in_array($ext, $allowed)));
    }

    return true;
}

/**
 * Makes sure a file field is required and not optional.
 *
 * @access public
 * @param Model $model
 * @param array $data
 * @param boolean $required
 * @return boolean
 */
public function required(Model $model, $data, $required = true) {
    foreach ($data as $fieldName => $field) {
        if ($required && (!is_array($field) || empty($field['tmp_name']))) {
            return false;
        }
    }

    return true;
}

/**
 * Build the validation rules and validate.
 *
 * @access public
 * @param Model $model
 * @return mixed
 */
public function beforeValidate(Model $model) {
    if (!empty($this->_settings[$model->alias])) {
        foreach ($this->_settings[$model->alias] as $field => $rules) {
            $validations = array();

            foreach ($rules as $rule => $setting) {
                $set = $this->_defaults[$rule];

                // Parse out values
                if (!isset($setting['value'])) {
                    $setting = array('value' => $setting);
                }

                switch ($rule) {
                    case 'required':    $arg = (bool) $setting['value']; break;
                    case 'extension':   $arg = (array) $setting['value']; break;
                    default:            $arg = (int) $setting['value']; break;
                }

                if (!isset($setting['rule'])) {
                    $setting['rule'] = array($rule, $arg);
                }

                if (isset($setting['error'])) {
                    $setting['message'] = $setting['error'];
                    unset($setting['error']);
                }

                unset($setting['value']);

                // Merge settings
                $set = array_merge($set, $setting);

                // Apply validations
                if (is_array($arg)) {
                    $arg = implode(', ', $arg);
                }

                $set['message'] = __d('uploader', $set['message'], $arg);

                $validations[$rule] = $set;
            }

            if (!empty($validations)) {
                if (!empty($model->validate[$field])) {
                    $validations = $validations + $model->validate[$field];
                }

                $this->_validations[$field] = $validations;
                $model->validate[$field] = $validations;
            }
        }
    }

    return true;
}

/**
 * Allow empty file uploads to circumvent file validations.
 *
 * @access protected
 * @param Model $model
 * @param string $fieldName
 * @param array $field
 * @return bool
 */
protected function _allowEmpty(Model $model, $fieldName, $field) {
    if (isset($this->_validations[$fieldName]['required'])) {
        $rule = $this->_validations[$fieldName]['required'];
        $required = isset($rule['rule'][1]) ? $rule['rule'][1] : true;

        if (empty($field['tmp_name'])) {
            if ($rule['allowEmpty']) {
                return true;

            } else if ($required) {
                return false;
            }
        }
    }

    return false;
}

/**
 * Validates multiple combinations of height and width for an image.
 *
 * @access protected
 * @param Model $model
 * @param array $data
 * @param string $type
 * @param int $size
 * @return boolean
 */
protected function _validateImage(Model $model, $data, $type, $size = 100) {
    foreach ($data as $fieldName => $field) {
        if ($this->_allowEmpty($model, $fieldName, $field)) {
            return true;

        } else if (empty($field['tmp_name'])) {
            return false;
        }

        $file = getimagesize($field['tmp_name']);

        if (!$file) {
            return false;
        }

        $width = $file[0];
        $height = $file[1];

        switch ($type) {
            case 'width':       return ($width == $size); break;
            case 'height':      return ($height == $size); break;
            case 'maxWidth':    return ($width <= $size); break;
            case 'maxHeight':   return ($height <= $size); break;
            case 'minWidth':    return ($width >= $size); break;
            case 'minHeight':   return ($height >= $size); break;
        }
    }

    return true;
}

}
    
asked by anonymous 07.05.2014 / 15:53

2 answers

1

You do not need the unset() in line 283 of the app/Plugin/Uploader/Model/Behavior/FileValidationBehavior.php file, the presence of offset 'error' will not interfere with the operation. The error is caused by you are trying to remove an offset in a variable in a foreach() .

The reason that this error happened only when you uploaded the project is the difference between the php versions of your machine and your approval environment (or production do not know)

    
07.05.2014 / 16:32
0

I think I found the solution in a problem .

    
07.05.2014 / 16:18