0

I'm having some issues with a form and it's driving me insane.

Whenever I try to upload an image to my database, I get

Notice: Array to string conversion [CORE\Cake\Model\Datasource\DboSource.php, line 1009] 

I'm not sure what I'm doing wrong or missing. Any help is appreciated.

This is my Model

        'banner_image' => array(
        'not_required' => array(
            'allowEmpty' => true,
            'required' => false,
        ),
        'is_image' => array(
            'rule' => 'is_image_check',
            'message' => 'We found that the file you uploaded is not an image.',
            //'allowEmpty' => false,
            'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
),

This is my controller

/**
 * admin_add method
 *
 * @return void
 */
public function admin_add() {
    if ($this->request->is('post')) {
        $this->Survey->create();
        if ($this->Survey->save($this->request->data)) {
            $this->Session->setFlash(__('The survey has been saved.'));
            return $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The survey could not be saved. Please, try again.'));
        }
    }
}

and this is my form

<?php echo $this->Form->create('Survey', array('type'=>'file')); ?>
<fieldset>
    <legend><?php echo __('Admin Add Survey'); ?></legend>
<?php 
    echo $this->Form->input('title');
    echo $this->Form->input('subtitle');

    if ( empty($this->request->data['Survey']['banner_image']) or isset($this->validationErrors['Survey']['banner_image']) ): 

            echo $this->Form->input('banner_image', array('type'=>'file'));

    else : 

        echo $this->Html->image('/img/surveys/' . $this->request->data['Survey']['banner_image'] ) ;                
        echo $this->Html->link('Remove this image?', '/admin/Surveys/remove_image/' . $this->request->data['Survey']['id'] ) ;
    endif;
    // echo $this->Form->input('listing_image', array('type'=>'file'));
    echo $this->Form->input('url_iframe');
    echo $this->Form->input('enable');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
4
  • When you post the form, banner_image will not contain the file content. It'll contain uploaded file information array. You can read about it here: POST method uploads Commented Jan 19, 2016 at 3:39
  • Please always mention your exact CakePHP version and tag your question accordingly - thanks! Commented Jan 19, 2016 at 7:32
  • @user3082321 Thanks for the link. It was a good read. Commented Jan 19, 2016 at 18:26
  • @ndm Sorry about that. Next time I'll remember that. Commented Jan 19, 2016 at 18:26

1 Answer 1

3

This is what your controller receives for field banner_image:

$this->request->data['Survey']['banner_image'] = array(
    'name' => 'example_image.jpg',
    'type' => 'image/jpg',
    'tmp_name' => 'C:/WINDOWS/TEMP/php1EE.tmp', //path will vary on Unix-like OSes
    'error' => 0,
    'size' => 41737,
);

If you attempt to save this into your table, you will get

Notice: Array to string conversion in filename on line X

Therefore, you have to do some pre-processing before you can call save().

Your surveys.banner_image is probably set to accept a file name.

A typical approach is to implement the Survey::beforeSave() callback and add the necessary code to move the uploaded file from its temporary location to the destination folder of your choice.

You then overwrite the $data['Survey']['banner_image'] array with $data['Survey']['banner_image']['name'].

Or, instead of reinventing the wheel, you can use one of the multiple CakePHP plugins which handles uploads, for example josegonzalez/cakephp-upload.

For further reference, see:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.