user1192304
user1192304

Reputation: 217

Cakephp - MeioUpload - How to download files uploaded

I just started to use meioupload to upload my files from cakephp, however, i'm not sure how do i code the download.

I tried using the following code:

echo $file['Image']['dir'].$file['Image']['filename'];

however, it seems that the code is wrong. as it output the following:

uploads\image\filenamedb_fyp_2.txt 

How do i download the file db_fyp2.txt?

Upvotes: 0

Views: 2048

Answers (3)

user1192304
user1192304

Reputation: 217

Thanks for the answer, but i already found out how to download the files using mediaview of cakephp.

Anyway, to answer my own question, to download the file with various extension, the following code can be used.

Using Media View of cakephp - in the controller

public function download($id){



     $this->viewClass = 'Media';
     $this->autoLayout = false;


    $file = $this->Image->findById($id); 
     $link = $file['Image']['dir'];



     $params = array(
     'id' => $file['Image']['filename'],
     'name' => $file['Image']['filename'],
     'download' => true,
     'mimeType' => $file['Image']['mimetype'], 
     'extension' => array('pdf','zip','txt'),
     'path' => $link.DS
     );

    $this->set($params);

Upvotes: 0

jack
jack

Reputation: 473

Try adding your root to where you are printing this.Only if its given a url it will work.And the main mitake you are doing here is you have to make it a link instead of just printing it.

$this->Html->link('Link Name',$path to that file);

Upvotes: 0

Oldskool
Oldskool

Reputation: 34837

Well, all you are telling your app is to echo the filepath and that's exactly what it's doing. If you just want the file opened in the browser, use the redirect control flow instead, like:

class YourController extends AppController {
    public function upload() {
        // You upload logic here, followed by ...
        $this->redirect($file['Image']['dir'].$file['Image']['filename']);
    }
}

If you want the browser to offer the file as a download, sent the appropriate Content-Disposition header (See Example #1).

header('Content-Disposition: attachment; filename="' . $file['Image']['filename'] . '"');
readfile($file['Image']['dir'].$file['Image']['filename']);

Upvotes: 0

Related Questions