Welcome Guest, Not a member yet? Register   Sign In
Need help getting Force Download to work
#1

[eluser]brent7779[/eluser]
Please excuse my newbieness, but this seems so very simple, i copied the code directly from the user guide and i can't understand why this function doesn't auto download as soon as the "download.php" controller is called up, i want to avoid the download being possible by using a direct url like - http://www.mysite.com/index.php/download/getpdf... so, I put the underscore in front of the function name and put that function inside of the index function for this controller, so what gives? nothing happens at all, I get no php errors or anything, its not the wrong path to file, just nothing happens...



<?php

class Download extends Controller {

function index() {

$this->load->helper('download');
$this->load->view('header_view');
$this->load->view('download_view');
$this->load->view('footer_view');

function _getpdf() {
$data = file_get_contents("../../filename.pdf"); // Read the file's contents
$name = 'filename.pdf';

force_download($name, $data);
}



}
}
?>
#2

[eluser]vitoco[/eluser]
First of all, was a bad copy paste...

1.- force the download on the the index method
Code:
<?php
  
// THIS WORK IF YOU CALL http://domain/download/[index]
class Download extends Controller {
  
  function index()
  {    
      $data = file_get_contents(”../../filename.pdf”); // Read the file’s contents
      $name = ‘filename.pdf’;
      //
      force_download($name, $data);
   }
}
?>

2.- force the download on the the get_pdf method, that will be called in a view
Code:
<?php
  
// THIS WORK IF YOU CALL http://domain/download/get_pdf/
class Download extends Controller {
  
  // shows a link to download the pdf
  function index()
  {
      echo base_url().'/download/get_pdf';
  }

  function get_pdf()
  {    
      $data = file_get_contents(”../../filename.pdf”); // Read the file’s contents
      $name = ‘filename.pdf’;
      //
      force_download($name, $data);
   }
}
?>
Saludos
#3

[eluser]brent7779[/eluser]
Thanks alot vitoco for your quick response, I can't wait to try your solution, I will post back as soon as I can try it!



Oh, one more thing, it was my understanding that in order to make a function private the underscore had to precede everything else, no?
"_getpdf()"
and if I call the method from a view, I can't use the underscore in the path cause it will give 404 error right?
Code:
<?php
  
// THIS WORK IF YOU CALL http://domain/download/_getpdf/
class Download extends Controller {
  
  // shows a link to download the pdf
  function index()
  {
      echo base_url().'/download/_getpdf';
  }

  function _getpdf()
  {    
      $data = file_get_contents(”../../filename.pdf”); // Read the file’s contents
      $name = ‘filename.pdf’;
      //
      force_download($name, $data);
   }
}
?>

I would like to avoid making the user click another link, they have already filled out my form and gone through paypal at this point, that's why I wanted the download method nested in the index method for that controller and auto download.

Thanks again, I really appreciate you guys helping out the newbies here!




Theme © iAndrew 2016 - Forum software by © MyBB