简体   繁体   中英

edit view without modifying file cakePHP

View(edit) is displaying all the fields correctly and updating modified fields but it cant handle files. ie, 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

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'];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM