Solved

Download a file

Posted on 2007-03-17
1
284 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
reverse engineer .sql from php files 11 52
php subtract from date 2 29
deprecated mysql extensions 1 27
if statement malfunction 5 23
This article discusses four methods for overlaying images in a container on a web page
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

749 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