kwangu

DOWNLOADED FILE NOT OPENING Adobe reader

Feb 27th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.33 KB | None | 0 0
  1. CONTROLLER
  2. //Load view file
  3. public function index(){
  4.            
  5.           $data['pdfs'] = $this->files_model->get_pdf_files();
  6.           $this->load->view('download/dynamic',$data);
  7.          
  8.         }
  9. // function to download file
  10. public function force_download($path, $name){
  11.   // make sure it's a file before doing anything!
  12.   if(is_file($path))
  13.   {
  14.     // required for IE
  15.     if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }
  16.  
  17.     // get the file mime type using the file extension
  18.     $this->load->helper('file');
  19.  
  20.     $mime = get_mime_by_extension($path);
  21.  
  22.     // Build the headers to push out the file properly.
  23.     header('Pragma: public');     // required
  24.     header('Expires: 0');         // no cache
  25.     header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  26.     header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($path)).' GMT');
  27.     header('Cache-Control: private',false);
  28.     header('Content-Type: '.$mime);  // Add the mime type from Code igniter.
  29.     header('Content-Disposition: attachment; filename="'.basename($name).'"');  // Add the file name
  30.     header('Content-Transfer-Encoding: binary');
  31.     header('Content-Length: '.filesize($path)); // provide file size
  32.     header('Connection: close');
  33.     readfile($path); // push it out
  34.     exit();
  35. }}
  36.     public function get_pdf_file(){
  37.         $path = file_get_contents(base_url()."/uploads/docs/"); // Read the file's contents
  38.         $data['files'] = $this->files_model->get_pdf_files();
  39.  
  40.         foreach($data['files'] as $row):
  41.             $name = $row->file_name;
  42.  
  43.             endforeach;
  44.         //$name = $data['files']['0']['file_name'];
  45.        
  46.         force_download($name,$path);
  47.        
  48.         }
  49. VIEW
  50. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  51. <html xmlns="http://www.w3.org/1999/xhtml">
  52. <head>
  53. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  54. <title>Download Document</title>
  55. </head>
  56.  
  57. <body>
  58.   <!--<a href="/download/get_file">Download</a>-->
  59.   <ul>
  60.   <?php
  61.   //print_r($pdfs);
  62.    foreach($pdfs as $row):
  63.            ?>
  64.            
  65.  <?php echo "<li style=\"padding-bottom:10px;\">
  66. <a href='/download/get_pdf_file/".$row->file_name."'>Download</a></li>";?>
  67.            <?php endforeach;?>
  68.            </ul>
  69.    
  70. </body>
  71. </html>
Add Comment
Please, Sign In to add comment