Skip to content

Instantly share code, notes, and snippets.

@guinslym
Last active January 2, 2016 15:09
Show Gist options
  • Save guinslym/8321232 to your computer and use it in GitHub Desktop.
Save guinslym/8321232 to your computer and use it in GitHub Desktop.
I have an error : "The Configuration could not be saved." when I'm uploading a file
<?php
App::uses('AppModel', 'Model');
/**
* Configuration Model
*
*/
class Configuration extends AppModel {
/**
* Display field
*
* @var string
*/
public $displayField = 'profession';
public $validate = array(
'userphoto' => array(
'uploadError' => array(
'rule' =>'uploadError',
'message' => 'Your image upload failed',
//'last' => false, // Stop validation after this rule
'on' => 'create' // Limit validation to 'create' or 'update' operations
),
'mimeType' => array(
'rule' => array('mimeType', array('image/gif', 'image/png', 'image/jpg', 'image/jpeg')),
'message' => 'Please only upload images (gif, png, jpg).',
'on' => 'create'
),
'fileSize' => array(
'rule' => array('fileSize', '<=', '2MB'),
'message' => 'Your image must be less than 2MB or(2048ko).',
'on' => 'create'
),
'processCoverUpload' => array(
'rule' => 'processCoverUpload',
'message' => 'Unable to process cover image upload.',
'on' => 'create'
),
'unique' => array(
'rule' => 'isUnique',
'message' => 'This file name is already exist in your folder',
'on' => 'create'
),
),
);
public function processCoverUpload($check = array()) {
if (!is_uploaded_file($check['userphoto']['tmp_name'])) {
return FALSE;
}
if (!move_uploaded_file($check['userphoto']['tmp_name'], WWW_ROOT . 'img' . DS . 'uploads' . DS . $check['userphoto']['name'])) {
return FALSE;
}
$this->data[$this->alias]['userphoto'] = 'uploads' . DS . $check['userphoto']['name'];
return TRUE;
}
}
<?php
App::uses('AppController', 'Controller');
/**
* Configurations Controller
*
* @property Configuration $Configuration
* @property PaginatorComponent $Paginator
*/
class ConfigurationsController extends AppController {
/**
* Components
*
* @var array
*/
public $components = array('Paginator');
function beforeFilter(){
parent::beforeFilter();
$this->Auth->allow('listConfigurations');
}
/**
* edit method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function edit($id = null) {
if (!$this->Configuration->exists($id)) {
throw new NotFoundException(__('Invalid configuration'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if(isset($this->request->data['Configuration']['userphoto'])){
$file = $this->request->data['Configuration']['userphoto'];
if ($file['error'] === UPLOAD_ERR_OK) {
$filename = $file['name'];
if (move_uploaded_file($file['tmp_name'], WWW_ROOT . 'img' . DS . 'uploads' . DS .$file['name'])) {
$this->request->data['Configuration']['userphoto'] = $filename;
}
}
if ($this->Configuration->save($file)) {
$this->Session->setFlash(__('The Configuration has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The Configuration could not be saved. Please, try again.'));
}
}
} else {
$options = array('conditions' => array('Configuration.' . $this->Configuration->primaryKey => $id));
$this->request->data = $this->Configuration->find('first', $options);
}
//render a view.
}
}// end configuration
<div class="configurations form">
<?php echo $this->Form->create('Configuration', array('type' => 'file')); ?>
<fieldset>
<legend><?php echo __('Edit Configuration'); ?></legend>
<?php
echo $this->Form->input('id');
echo $this->Form->input('username');
echo $this->Form->input('profession');
echo $this->Form->input('description', array('type' => 'textarea','label' => 'Content of this Article', 'rows' => '10', 'cols' => '120'));
echo $this->Form->input('userphoto', array('type' => 'file'));
echo $this->Form->input('tel_mobile');
echo $this->Form->input('address');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
<div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $this->Form->value('Configuration.id')), null, __('Are you sure you want to delete # %s?', $this->Form->value('Configuration.id'))); ?></li>
<li><?php echo $this->Html->link(__('List Configurations'), array('action' => 'index')); ?></li>
</ul>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment