Welcome Guest, Not a member yet? Register   Sign In
Forcing download PDF Files not working
#1

[eluser]Isern Palaus[/eluser]
Hello,

I'm trying to force download PDF Files from a controller.

In the administration of the website that I'm programing it's possible to add PDF files. Well, in the public version it needs to be download and I tried this:

Code:
function getRevista()
{
    $id = $this->uri->segment(2, 0);
    
    if($id == TRUE) {
        $this->load->helper('download');
        
        $fileName     = "2b07f.pdf";
        $path         = "revistes/";
        
        force_download($fileName,$path.$fileName);
    }
}

The filename usually will be asked in the DB but now, for the proof of concept I use a specific name. The uploaded files are in ./public_html/revistes/ my aplication folder is ./application/ and the system is ./system/.

When I try to get the file Adobe Reader gives me a error like:
Quote:Acrobat could not open '2b07f.pdf' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly uploaded.
[...]

When I checked the file for the size i saw that it's not a 0 bytes files. It contains the URL to the download, like this:
http://argentona.ipalaus.es/revistes/2b07f.pdf

What can I do?

Thank you in advance and sorry for school English.

Regards,
-- Isern Palaus
#2

[eluser]Isern Palaus[/eluser]
What's more, what I want is that the user can't never know the real URL to the file.

By the way, it's possible to put the files before the public_html folder? And force a download to it like

/home/ipalaus/iesargentona/revises/2b07f.pdf

for the controller in:

/home/ipalaus/iesargentona/application/controllers/main.php

and the site:

/home/ipalaus/iesargentona/public_html/index.php

Sorry for double replying.

Regards,
-- Isern Palaus
#3

[eluser]dawnerd[/eluser]
It is very possible and gave me much trouble when I first attempted it.

Code:
function _readfile_chunked($filename,$retbytes=true) {
       $chunksize = 1*(1024*1024); // how many bytes per chunk
       $buffer = '';
       $cnt =0;
       // $handle = fopen($filename, 'rb');
       $handle = fopen($filename, 'rb');
       if ($handle === false) {
           return false;
       }
       while (!feof($handle)) {
           $buffer = fread($handle, $chunksize);
           echo $buffer;
           ob_flush();
           flush();
           if ($retbytes) {
               $cnt += strlen($buffer);
           }
       }
           $status = fclose($handle);
       if ($retbytes && $status) {
           return $cnt; // return num. bytes delivered like readfile() does.
       }
       return $status;
    
    }

That's the best way in case you have a larger pdf. As for the path, it is relative to the CI root. Meaning if it is below pub_html then you must use ../downloads/file.

In the class I supplied above, the first parameter is the path to the file with the filename at the end.

I hope this helped.
#4

[eluser]Alex007[/eluser]
force_download() 2nd parameter must be the file CONTENT, not the file name. Read your whole PDF in a string variable, then pass that to force_download().

Re-read the user guide, there's a perfect example in there.




Theme © iAndrew 2016 - Forum software by © MyBB