Welcome Guest, Not a member yet? Register   Sign In
How to hide a remote file download link?
#1

[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!
#2

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

http://ellislab.com/codeigniter/user-gui...elper.html
#3

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

http://ellislab.com/codeigniter/user-gui...elper.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.




Theme © iAndrew 2016 - Forum software by © MyBB