I am using cakephp 2x. I have a problem in my edit page image upload field. Everytime when i try to edit my profile page , it shows image upload field blank.Other fields information are remains same as fill in the add form of profile but the image field is not enable to retrieve stored image. I am new to cakephp,if anyone knows how i can retrieve image? plz help! And i also want to know how i can store more than one image and also check the same image is not uploading twice. Thanks! Here is my image upload code->
// File upload function in Model.php
public $validate = array(
'image' => array(
'uploadError' => array(
'rule' => 'uploadError',
'message' => 'Something went wrong with the file upload',
'required' => FALSE,
'allowEmpty' => TRUE,
),
// http://book.cakephp.org/2.0/en/models/data- validation.html#Validation::mimeType
// custom callback to deal with the file upload
'imageOne' => array(
'rule' => 'imageOne',
'message' => 'Something went wrong processing your file',
'required' => FALSE,
'allowEmpty' => TRUE,
'last' => TRUE,
),
),
);
public function imageOne($check=array()) {
// deal with uploaded file
if (!empty($check['image']['tmp_name'])) {
// check file is uploaded
if (!is_uploaded_file($check['image']['tmp_name'])) {
return FALSE;
}
// build full filename
$filename = WWW_ROOT . $this->uploadDir . DS . Inflector::slug(pathinfo($check['image']['name'], PATHINFO_FILENAME)).'.'.pathinfo($check['image']['name'], PATHINFO_EXTENSION);
// @todo check for duplicate filename
/* if (!file_exists($check['image']['tmp_name'],$filename)) {
echo "Sorry, file already exists.";
}*/
// try moving file
if (!move_uploaded_file($check['image']['tmp_name'], $filename)) {
return FALSE;
// file successfully uploaded
} else {
// save the file path relative from WWW_ROOT e.g. uploads/example_filename.jpg
$this->data[$this->alias]['filepath'] = str_replace(DS, "/", str_replace(WWW_ROOT, "", $filename) );
}
}
return TRUE;
}
public function beforeSave($options = array()) {
// a file has been uploaded so grab the filepath
if (!empty($this->data[$this->alias]['filepath'])) {
$this->data[$this->alias]['image'] = $this->data[$this->alias] ['filepath'];
}
// Controller.php
public function edit($id = null) {
if (!$this->CollegeProfile->exists($id)) {
throw new NotFoundException(__('Invalid college profile'));
}
if ($this->request->is(array('post', 'put'))) {
$this->request->data['CollegeProfile']['user_id'] = $this->Auth- >user('id');
$this->request->data['CollegeProfile']['id'] = $id;
if ($this->CollegeProfile->save($this->request->data)) {
$this->Session->setFlash(__('The college profile has been saved.'), 'default', array('class' => 'alert alert-success'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The college profile could not be saved. Please, try again.'), 'default', array('class' => 'alert alert-danger'));
}
} else {
$options = array('conditions' => array('CollegeProfile.' . $this->CollegeProfile->primaryKey => $id));
$this->request->data = $this->CollegeProfile->find('first', $options);
}
$this->loadModel('State');
$states = $this->State->find('list');
$this->set(compact('states'));
}
//Edit.ctp
<div class="form-group">
<?php echo $this ->Form->input('image',array('class'=>'form-control','type'=>'file'));?>
</div>
There's no way to do this.
See related question: Dynamically set value of a file input.
Most browsers block against setting the value attribute on input type file for security reasons so that you can't upload a file without the user's input.
However, in the interests of showing the user what they last selected, you can do something like this;
<?php
// edit.ctp
if (!empty($this->request->data['Model']['image'])) {
$label = "Current file: " . $this->request->data['Model']['image'];
} else {
$label = __("Select file");
}
echo $this->Form->input('Model.image', array('type' => 'file', 'label' => $label));