Reply to Re: Allowing users to download files using headers

Your name:

Reply:


Posted by ZeldorBlat on 12/05/06 02:35

mpar612 wrote:
> I am trying to create an area where a user can come to a page that
> queries a database and displays links to files. Here is the code for
> the link:
>
> <td><a href="download.php?path=../ftp/$path&name=$name\"
> target=\"_blank\">$name</a></font></b></td>

Ok, that's a start.\

>
> >From what I have read the purpose of download.php is to contain the
> header information to download the file.

That's part of what it should do. The other part is to actually send
the contents of the file.

>This is the only code that I
> have in download.php:
>
> <?php
> $file = $_GET['file'];
> $name = $_GET['name'];
> header("Content-Type: application/octet-stream");
> header("Content-Disposition: attachment; filename=$name");
> ?>
>
> When I click on the original link above a window pops up with the
> correct filename asking me if I want to open or save the file, but when
> I save it to a disk or try opening it, I do not get the file that is on
> the server. It is simply a blank file or I get an error from the
> program that is trying to open the file.

Right, because, although you've sent the headers, you haven't sent any
of the content.

>
> What I don't understand is how does download.php know which file to
> open? Am I taking the right approach to this?

It doesn't! After you send the headers you need to somehow get the
contents of the file and send (echo) it to the browser. Something like
this should work, but you probably need to think a little bit about
security issues (like scrubbing the value of 'file' and 'name' and
making sure people can't get something they shouldn't):

echo file_get_contents($path);

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация