【发布时间】:2017-05-25 06:19:15
【问题描述】:
我正在使用 cakephp 2x。我的编辑页面图片上传字段有问题。每次我尝试编辑我的个人资料页面时,它都会显示图像上传字段为空白。其他字段信息与填写个人资料的添加表单相同,但图像字段无法检索存储的图像。我是 cakephp 新手,如果有人知道我如何检索图像?请帮忙!而且我还想知道如何存储多个图像并检查同一个图像没有上传两次。谢谢!这是我的图片上传代码->
// 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>
【问题讨论】: