dongye3917 2013-09-10 10:06
浏览 19
已采纳

编辑视图而不修改文件cakePHP

View(edit) is displaying all the fields correctly and updating modified fields but it cant handle files. i.e., if new file is uploaded then it has to be updated if not old file name has to be retained.

controller:

public function edit($id = null) {
    if (!$id) {
        throw new NotFoundException(__('Invalid post'));
    }

    $post = $this->Student->findById($id);
    if (!$post) {
        throw new NotFoundException(__('Invalid post'));
    }

    if ($this->request->is('post') || $this->request->is('put')) {
        $this->Student->id = $id;
        if ($this->Student->save($this->request->data)) {
            $this->Session->setFlash(__('Your post has been updated.'));
            return $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Unable to update your post.'));
    }

    if (!$this->request->data) {
        $this->request->data = $post;
    }
    }

View:

<h1>Edit student record</h1>
<?php   
    echo $this->Form->create('Student',array('type'=>'file'));
    echo $this->Form->input('first_name'); 
    echo $this->Form->input('current_address');
    echo 'resume'.$this->Form->file('resume');
    echo $this->Form->input ('comments');
    echo 'photo'.$this->Form->file('photo'); 
    echo $this->Form->input('id', array('type' => 'hidden'));
    //echo $this->Form->input('resume', array('type' => 'hidden'));
    //echo $this->Form->input('photo', array('type' => 'hidden'));
    echo $this->Form->end('Save Post');

Can someone suggest me how to handle the upload thing

  • 写回答

1条回答 默认 最新

  • dounuo9921 2013-09-10 10:37
    关注

    Check resume, photo fields. If it's empty, unset variable. If not empty - upload and move to your destination:

    if (empty($this->request->data['Student']['resume']['name'])) {
        unset($this->request->data['Student']['resume']);
    } else {
        $resume = $this->request->data['Student']['resume'];
        move_uploaded_file($resume['tmp_name'], 'newPath/' . $resume['name']);
        $this->request->data['Student']['resume'] = $resume['name'];
    }
    
    if (empty($this->request->data['Student']['photo']['name'])) {
        unset($this->request->data['Student']['photo']);
    } else {
        $photo = $this->request->data['Student']['photo'];
        move_uploaded_file($photo['tmp_name'], 'newPath/' . $photo['name']);
        $this->request->data['Student']['photo'] = $photo['name'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi