【问题标题】:on editing file upload field is blank编辑文件上传字段为空白
【发布时间】: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>

【问题讨论】:

    标签: image cakephp


    【解决方案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));
    

    【讨论】:

    • 是的,@Robmcvey,我的编辑表单包括 type => 文件。
    • 以上一切正常,我的问题是添加配置文件后,当我点击编辑时,我的文件上传字段为空白,我可以从我的编辑表单重新上传文件,但主要问题是之后当我继续编辑我的文件上传字段时添加至少会显示我在添加表单中上传的上传文件的名称 - 它不应为空。
    【解决方案2】:

    这是不可能的,您可以在保存之前获取数据,比较字段值是否为空,然后重新填充您之前获取的数组。 例如

    $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 数组值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-26
      • 1970-01-01
      • 2013-08-22
      • 1970-01-01
      • 2015-10-27
      • 1970-01-01
      相关资源
      最近更新 更多