Thursday, April 11, 2013

Re: Download file

Dude, that´s why i used the variables $name and $path, because it´s dynamic, and you should use at controller´s action, i will give an example:

//Assuming that the folders structure are exactly the same it cames from CakePHP´s server.
class DownloadsController extends AppController{    
    public function download($name){
        //First check if user is logged in or any check you want to do before it downloads...
        $path=APP.WEBROOT_DIR.DS.'files'.DS.$name;
        header("Content-Type: application/octet-stream");
        header("Content-Transfer-Encoding: Binary");
        header('Content-disposition: attachment; filename="'.$name.'"'); 
        echo file_get_contents($path);
    }    
}

Them if you access www.yoursite.com/downloads/download/file.extension it will download the file.extension, and the system will get this file at /app/webroot/files dir
Or if you navigate to www.yoursite.com/downloads/download/mypicture.jpg it will force the download of mypicture.jpg, wich should be at /app/webroot/files dir

***
If the download should start ONLY if the user is logged-in or if user should do some action, do not use the webroot´s directory to store the files, use the app directory or any directory not accessible throught browser
This is just an example to understand how to force a download in CakePHP, it´s just adjust for your use.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate