Invalid argument supplied for foreach()

Asked

Viewed 519 times

-1

I get this mistake:

Warning (2): Invalid argument supplied for foreach() [CORE Cake Utility Validation.php, line 490]

using the admin_upload_image action from my Galleries controller. But this action was working correctly, and I didn’t modify it, except to put parameters for upload validation.

Controller

    public function admin_upload_image(){
        /*if(!$this->Session->check('Admin')) {
            $this->Session->setFlash('Está a aceder a uma zona restrita. Por favor faça Login.');
            $this->redirect(array(
                            'controller' => 'admins',
                            '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['gallery_images']['path']['name']
                                        )
                );
            move_uploaded_file($this->data['gallery_images']['path']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/html/PushUp_app/app/webroot/img/gallery/' . $this->data['gallery_images']['path']['name']);

            $this->loadModel('GalleryImage');
            $this->GalleryImage->create();
            if($this->GalleryImage->save($file)){
                $this->Session->setFlash(__('Ficheiro carregado com sucesso!'));
            }
            else{
                $this->Session->setFlash(__('Erro ao carregar o ficheiro!'));
            }
        }
    }

View

<h2>Adicionar Fotografia</h2>
<?php
    echo "<br>";
    echo $this->Form->create('GalleryImage',array('type'=>'file'));
    echo $this->Form->file('gallery_images.path');
    echo "<br>";
    //echo $this->Form->submit();
    echo $this->Form->end('Guardar');
?>

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(
            'rule' => array(
                'allowEmpty' => true,
                'extension', array('gif', 'jpeg', 'png', 'jpg', 'tiff'),
                'fileSize', '<=', '40MB' 
                ),
            'message' => 'ERRO, a fotografia deve ter um tamanho inferior a 40MB.'
            )
    );
}
?>

1 answer

1


Its variable $validate is declaring the arrays incorrectly. Do so so:

var $validate = array(
    'path' => array(
        'rule' => array(
            'allowEmpty' => true,
            'extension' => array('gif', 'jpeg', 'png', 'jpg', 'tiff'),
            'fileSize' => '40MB' 
        ),
        'message' => 'ERRO, a fotografia deve ter um tamanho inferior a 40MB.'
    )
);
  • The 'filesize' => '40MB' makes it necessary to have a size of 40MB or less? No book of Cakephp appears like this : 'rule' => array('fileSize', '<=', '1MB').

  • @Sunt good, Cakephp itself doesn’t have an upload method, maybe you should be using a library, then you have to see what the specification for this is. What I had noticed was the assignment of an array, something standard of PHP.

Browser other questions tagged

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