hi all,

i have successfully created a download script to force a user to
download, however attempting to download large files causes an error
saying that the file cannot be found.

my code >
header("Cache-control: private");
header("Content-Type: application/force-download; name=\"$file\"");
header("Content-Disposition: attachment; filename=\"$file \"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: $content_length");
$fp = fopen($file_fullpath,"r");
fpassthru($fp);
fclose($fp);
< my code

this is a memory issue in the php.ini i.e. memory_limit = 8M then the
largest file i can download is 8M

is there anyway to "force" a download without having to use the system
hungry fpassthru function? (or readfile etc functions)

this is driving me nuts so any help would be greatly appreciated

cheers
christian

ps i read the following at php.net fpassthru man page but could not make
sense of it (it appears to be some kind of solution) >

"fpassthru() works best for small files. In download manager scripts,
it's best to determine the URL of the file to download (you may generate
it locally in your session data if you need so), and then use HTTP
temporary redirects (302 status code, with a "Location:" header
specifying the effective download URL).
This saves your web server from maintaining PHP scripts running for long
times during the file downloadn and instead the download will be managed
directly by the web server without scripting support (consequence: less
memory resources used by parallel downloads)..."

    Could you use the header(Location: blah) to do this?

    I have not tries it but it would be worth a go.

      ideally it would be good not use the header location as files outside the webtree should be able to be pulled dynamically.

      this is not entirely necessary but event header location function appears not to "force" the download

        Hmmm - personally I have never had a problem just using "readfile" to dump the contents to the browser since all file content is passed directly to the browser and not stored in memory (afaik). However, I could be wrong.

        Surely, you could just use a simple while loop to dump the file contents out anyhow?

        i.e.

        $fp = fopen($file_fullpath,"r");
        
        while (!feof($fp))
        {
            print(fread($fp, 4000));
            flush(); // Probably don't need this, but it will stop PHP buffering any output until a new line flushes the buffer
        }
        
        fclose($fp);
        

        of course - you could always just passthru the file.....

        i.e.

        passthru('cat ' . $filename);
        

        🙂

          Write a Reply...