我正在使用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>
答案 0 :(得分:1)
没有办法做到这一点。
请参阅相关问题:Dynamically set value of a file input。
出于安全原因,大多数浏览器会阻止在输入类型文件上设置value属性,这样您就无法在没有用户输入的情况下上传文件。
但是,为了向用户显示他们上次选择的内容,您可以执行以下操作;
<?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));
答案 1 :(得分:0)
这是不可能的,相反,您可以在保存之前获取数据,比较字段值是否为空并重新填充您之前获取的数组。 例如
$data=$this->Option->findById(1);
//pic slide1
$slide1=$this->image($this->request->data['Option']['slide1']);(this is the upload)
if($slide1){ $this->request->data['Option']['slide1']=$slide1; }else{$this->request->data['Option']['slide1']=$data['Option']['slide1'];}
在这种情况下,我有一个“图像函数”,它返回上传的名称,所以如果名称为空,我将请求数据pic1值设置为我之前获取的$ data数组值。