Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CONTROLLER
- //Load view file
- public function index(){
- $data['pdfs'] = $this->files_model->get_pdf_files();
- $this->load->view('download/dynamic',$data);
- }
- // function to download file
- public function force_download($path, $name){
- // make sure it's a file before doing anything!
- if(is_file($path))
- {
- // required for IE
- if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }
- // get the file mime type using the file extension
- $this->load->helper('file');
- $mime = get_mime_by_extension($path);
- // Build the headers to push out the file properly.
- header('Pragma: public'); // required
- header('Expires: 0'); // no cache
- header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
- header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($path)).' GMT');
- header('Cache-Control: private',false);
- header('Content-Type: '.$mime); // Add the mime type from Code igniter.
- header('Content-Disposition: attachment; filename="'.basename($name).'"'); // Add the file name
- header('Content-Transfer-Encoding: binary');
- header('Content-Length: '.filesize($path)); // provide file size
- header('Connection: close');
- readfile($path); // push it out
- exit();
- }}
- public function get_pdf_file(){
- $path = file_get_contents(base_url()."/uploads/docs/"); // Read the file's contents
- $data['files'] = $this->files_model->get_pdf_files();
- foreach($data['files'] as $row):
- $name = $row->file_name;
- endforeach;
- //$name = $data['files']['0']['file_name'];
- force_download($name,$path);
- }
- VIEW
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
- <title>Download Document</title>
- </head>
- <body>
- <!--<a href="/download/get_file">Download</a>-->
- <ul>
- <?php
- //print_r($pdfs);
- foreach($pdfs as $row):
- ?>
- <?php echo "<li style=\"padding-bottom:10px;\">
- <a href='/download/get_pdf_file/".$row->file_name."'>Download</a></li>";?>
- <?php endforeach;?>
- </ul>
- </body>
- </html>
Add Comment
Please, Sign In to add comment