CodeIgniter Forums
How to hide a remote file download link? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How to hide a remote file download link? (/showthread.php?tid=55932)



How to hide a remote file download link? - El Forum - 11-17-2012

[eluser]Unknown[/eluser]
Hey all,

I've started using codeigniter a few weeks ago and so far, I'm loving it!

To get to the point:

I have an upload/download script. Files are uploaded to my VPS (other server), while the script is on a shared hosting.

The link to the file is saved in the database. I want users to download the file without viewing my VPS' link/real file location. A method would be CURL I thought, I used some tutorials and it never seems to work.

Problem: I get gibberish on the screen because the header type is never correct, even if I put image/png it will show gibberish instead of the image.

My code is here: (where forced to be image/png to test with pngs which actually didn't work)
Code:
function download_file($file)
{
  $this->db->where('id', $file);
  $query = $this->db->get('files');
  
  if($query->num_rows < 1)
   return false;
  
  $query = $query->result();
  
  $path = $query[0]->link; // The link to the file on the VPS server
  set_time_limit(0);
  
  $this->output->set_header("Pragma: public");
  $this->output->set_header("Expires: 0");
  $this->output->set_header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  $this->output->set_header("Cache-Control: private",false);
  $this->output->set_header("Content-Disposition: attachment; filename=" . basename($query[0]-&gt;link));  
  $this-&gt;output-&gt;set_header("Content-Transfer-Encoding: binary");
  $this->output->set_header("Content-Type: image/png"); //Doesn't even work for png images!
  
  $ch = curl_init($path);
  //Doesn't seem to work either $mime = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
  //$this->output->set_header("Content-Type: " . $mime);
  curl_exec($ch);
  curl_close($ch);
  exit();
}

Thanks!


How to hide a remote file download link? - El Forum - 11-17-2012

[eluser]codeartist[/eluser]
have you tried using this helper first ?

http://ellislab.com/codeigniter/user-guide/helpers/download_helper.html


How to hide a remote file download link? - El Forum - 11-17-2012

[eluser]Unknown[/eluser]
[quote author="codeartist" date="1353173503"]have you tried using this helper first ?

http://ellislab.com/codeigniter/user-guide/helpers/download_helper.html[/quote]

Awesome, looks just like what I need!

Thanks Smile

Edit: INevermind it does work! Turns out CURL was echoing the contents which was why headers were already sent Smile.