Thursday, April 3, 2014

Miles Johnson Uploader saves corrupt (empty) images

I'm using the Miles Johnson Uploader to upload and resize images. I'm finding that images are being uploaded to the correct location and the respective database table column is being updated with the correct path and file name, however, the images that are being saved to the server directory are effectively empty and corrupt (i.e. I upload a 500KB image, that ends up being 6KB in size once uploaded, and when I go to open it, I get the file is corrupt message). 
Note:  I've removed the resize config temporarily whilst I have this corrupt image issue, hence there's no config below for it.

I've provided the relevant code snippets below, but any advice to help resolve this issue around why my app's uploading empty/corrupt images is much appreciated.

Here is the upload code in my model file:

At the top of the model file:

App::uses('AppModel', 'Model','AttachmentBehavior');

And the configuration further down the model file:

public $actsAs = array(
'Uploader.Attachment' => array(
'image' => array(
'prepend' => 'logo-',
'tempDir' => TMP,
'uploadDir' => '/full-path-to/app/webroot/img/uploads/',
'finalPath' => '/img/uploads/',
'dbColumn' => 'image',
'overwrite' => true,
'stopSave' => true,
'allowEmpty' => true
)
),
'Uploader.FileValidation' => array(
       'image' => array(
           'extension' => array('gif', 'jpg', 'png', 'jpeg'),
           'type' => array('image'),
           'filesize' => 5242880
       )
)
);

Here is the code for the image input field only (fyi - the image field is one of many in the form) in the view. I'm using Bootstrap, hence the additional formatting:

echo $this->Form->input('image', array(
'fieldset' => false,
'label' => false,
'before' => '<label class="col-sm-2 control-label">Image</label><div class="col-sm-9">',
'after' => '</div>',
        'class' => 'form-control',
        'type' => 'file',
'div' => 'form-group',
'error' => array(
         'attributes' => array(
             'wrap' => 'div', 'class' => 'alert alert-danger'
                     )
                 )
)
); 


Finally, here is the controller code:

public function edit($id = null) {
if (!$this->ModelName->exists($id)) {
throw new NotFoundException(__('Invalid ModelName'));
}
if ($this->request->is(array('post', 'put'))) {
if ($this->ModelName->save($this->request->data)) {
$this->Session->setFlash(__('The ModelName has been saved.'), 'default', array('class' => 'alert alert-success'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The ModelName could not be saved. Please, try again.'), 'default', array('class' => 'alert alert-danger'));
}
} else {
$options = array('conditions' => array('ModelName.' . $this->ModelName->primaryKey => $id));
$this->request->data = $this->ModelName->find('first', $options);
}
}

Thanks!

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: