Solved

Download a file

Posted on 2007-03-17
1
273 Views
Last Modified: 2008-03-06
I would like to create a download link to a file outside the web server area, for example in the /tmp/

I could create a list of the files, but I dont know how to make it downloadable.

So far, I can create this link :

<a href="get_file.php?file=$file> $file </a>

Anyone know how to code the "get_file.php" so it will download the $file?

The $file is not in the web server area, for example /tmp/test.txt

I tried it like this (see below), but it does not work:

<?php

  header("Content-type: application/octet-stream");
  header("Content-disposition: attachment; filename=$file");

  $file = $_REQUEST['file'];
 
  echo $file;
 
  if (substr($file,0,5) != '/tmp/') {
     readfile($file);
  } else {
     echo "Error reading file";
  }

?>


Thanks!
0
Comment
Question by:nova2002
1 Comment
 
LVL 49

Accepted Solution

by:
Roonaan earned 125 total points
ID: 18739659
<?php

  $file = $_REQUEST['file'];

  $file = str_replace('..', '', $file); //prevent people from entering ../../.htaccess or something

  $path = '/tmp/'. $file;

  if(is_file($file)) {
    header("Content-type: application/octet-stream");
    header("Content-disposition: attachment; filename=".basename($file));
     readfile($file);
  } else {
     echo "Error reading file";
  }
?>

Then make sure that you use a relative path as your get_file.php argument:
For /tmp/file1.pdf, it would be get_file.php?file=file1.pdf
For /tmp/sub/file2.pdf, it would be get_file.php?file=sub/file2.pdf

Hope this helps,

-r-
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Importing and exporting data Magento 1.x ? 4 38
How to Insert a File Using Text Editor 9 55
php documentation 4 22
sql sentence 2 13
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now