简体   繁体   中英

CakePHP 2.0 File Download

I've been looking for how to download files stored in a webserver. However, I was not able to find what I'm looking for.

I have my files uploaded to the webserver (This is working). The destination path is stored in the database.

I want to be able to make a list of downloadable files, with the title of the filename as a download link.

Here's the code for the upload method, which is under Staff controller:

public function upload() {
        $this->layout = 'staff';
        $this->set('upload', $this->StaffUpload->find('all'));
        if($this->request->is('post')){
            $file = $this->request->data['Upload']['browsefile'];
            $id = $this->Auth->User('usersId');
            $staff = $this->Staff->find('first', array('conditions' => array('usersId' => $id)));
            $dest_file = ROOT . DS . 'app' .DS. 'staff_uploads' . DS . $file['name'];
            if(!is_dir(ROOT . DS . 'app' .DS. 'staff_uploads')) {
                mkdir(ROOT . DS . 'app' .DS. 'staff_uploads');
            }
            if(move_uploaded_file($file['tmp_name'], $dest_file)) {
                $this->StaffUpload->create();
                $data = array(
                    'title' => $this->request->data['Upload']['filetitle'],
                    'dest' => $dest_file,
                    'description' => $this->request->data['Upload']['filedescription'],
                    'type' => $this->request->data['Upload']['uploadtype'],
                    'title' => $this->request->data['Upload']['filetitle'],
                    'staffId' => $staff['Staff']['id'],
                    'date_uploaded' => date('Y-m-d H:i:s')
                );
                if($this->StaffUpload->save($data)) {
                    $this->Session->setFlash('The file has been successfully uploaded!', 'alert_box', array('class' => 'alert-success'), 'uploads');
                    $this->redirect(array('action' => 'upload'));
                }
            }

        }

    }

The code above is working fine. Now, I want to be able to download them with the title as their download link.

I want the download links to appear in this view, resources.ctp:

  <h1>Resources</h1> <hr/> </div><!-- end well --> </div><!-- end col-12 --> </div><!-- end bigCallout --> 

and this function, under the Website Controller:

public function resources() {
        $this->layout = 'website';
    }

I'm not sure what to put in that function. And I know that I also have to make another function. I'm new to CakePHP and any help is appreciated! I did not use the Media View as it has deprecated.

Thank you!

You can use below function to download the media files

public function download() {
    $this->viewClass = 'Media';
    // Render app/webroot/files/example.docx
    $params = array(
        'id'        => 'example.docx',
        'name'      => 'example',
        'extension' => 'docx',
        'mimeType'  => array(
            'docx' => 'application/vnd.openxmlformats-officedocument' .
                '.wordprocessingml.document'
        ),
        'path'      => 'files' . DS
    );
    $this->set($params);
}

Visit http://book.cakephp.org/2.0/en/views/media-view.html

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