views:

86

answers:

1

Hello, with the following code, small files are served fine, however large (see, 800MB and above) result in empty files!

Would I need to do something with apache to solve this?

    <?php


    class Model_Download { 


        function __construct($path, $file_name) {


$this->full_path = $path.$file_name;
    }


    public function execute() {

        if ($fd = fopen ($this->full_path, "r")) {
            $fsize      = filesize($this->full_path);
            $path_parts = pathinfo($this->full_path);
            $ext        = strtolower($path_parts["extension"]);

            switch ($ext) {
                case "pdf":
                    header("Content-type: application/pdf"); // add here more headers for diff. extensions
                    header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
                break;
                default;
                    header("Content-type: application/octet-stream");
                    header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
                break;
            }

            header("Content-length: $fsize");
            header("Cache-control: private"); //use this to open files directly

            while(!feof($fd)) {
                $buffer = fread($fd, 2048);
                echo $buffer;
            }
        }
        fclose ($fd);
        exit;
    }


}

Edit: If I use

            fpassthru($fd); exit;

instead, I get the following written inside a file:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 786032641 bytes) in /Users/aaron/Sites/com/library/Model/Download.php on line <i>44
A: 

I have gone the route of using mod_xsendfile, I have wrote a seperate script that has the following:

        $the_clip = 'files/Clip/'.$clip_bought->file_url;
        header('X-Sendfile: '.$the_clip);
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; file="'.$the_clip.'"');
        exit;

It serves the file, the download works, however it serves the file as download.php (the filename of the script) and not the name of the actual file.

Does anyone know a header that lets you serve the filename as you would like it?

Thanks

Edit:

Using Live header I can see its doing:

HTTP/1.1 200 OK
Date: Tue, 06 Apr 2010 10:42:28 GMT
Server: Apache/2.2.9 (Debian) DAV/2 SVN/1.5.1 PHP/5.2.6-1+lenny6 with Suhosin-Patch
X-Powered-By: PHP/5.2.6-1+lenny6
Content-Disposition: attachment; file="movie.wmv"
Last-Modified: Fri, 02 Apr 2010 13:09:32 GMT
Content-Length: 1107113956
Etag: "d8084-41fd37e4-48340b0ab3b00"
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: application/octet-stream

...but still serves the file as download.php

azz0r
After researching, $the_clip on line 4 is now the name of the file and not the path to it, however it still gets served as download.php, I'm guessing it must be a mod_xsendfile issue ?
azz0r