Why instead of doing a redirect to the file you handle it the Cake way? For
example, build an action called download in a controller called downloads.
Something like:

<?php

class DownloadsController extends AppController
{
        var $name = 'Downloads';
        var $downloadPath = WWW_ROOT . DS . 'upload';

        function download($file)
        {
                $filePath = $this->downloadPath . DS . $file;

                // Check against path injection

                if (preg_match('/^[A-Za-z0-9_\-\.]*$/', $file) &&
is_readable($filePath))
                {
                        $size = @filesize($filePath);

                        if (!headers_sent())
                        {
                                if ($size > 0)
                                {
                                        header('Content-length: ' . $size);
                                }

                                header('Content-Type:
application/octet-stream');
                                header('Content-Disposition: attachment;
filename='.$file);
                        }

                        readfile($filePath);
                        exit;
                }

                return $this->redirect('/');
        }
}

?>

Then when you build links to your files you do them like:

<a href="/downloads/download/my_file.txt">Download</a>

Or you can also add the following route to your app/config/routes.php file:

$Route->connect('/download/*', array('controller' => 'downloads', 'action'
=> 'download'));

And then build links like:

<a href="/download/my_file.txt">Download</a>

-MI

---------------------------------------------------------------------------

Remember, smart coders answer ten questions for every question they ask. 
So be smart, be cool, and share your knowledge. 

BAKE ON!


-----Mensaje original-----
De: [email protected] [mailto:[EMAIL PROTECTED] En nombre
de rainbow686
Enviado el: Domingo, 03 de Diciembre de 2006 10:57 p.m.
Para: Cake PHP
Asunto: what can I do, when I want download files form cake build system.


I'm a newer in cakephp. I want to build a download system with cake.
I host the file at WWW_ROOT.DS.'upload'.DS.$filename
(eg:www.aaa.com/upload/aaa.txt).
I use the code
      $this->redirect("/upload/$file_name");
to download the file.


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups 
"Cake PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to