Secure Downloading Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ruski
Forum Commoner
Posts: 28
Joined: Thu May 26, 2005 3:45 am

Secure Downloading Problem

Post by Ruski »

Im using a fgets function to download a specified file from my website, but instead of downloading it, it echos it below the button i press to download.

Site: http://www.rscheetah.com/download.php
(click on Cheetah1.0 and see for yourself)

Here is the php script that i made:

Code: Select all

<FORM method="post" action="downloads.php?file=p1.gif" name="form1">
<input type="submit" name="Submit" value="Cheetah 1.0">
</FORM>
<?php
if($file != "")
{	
$fp = fopen('./downloads/'.$file, 'r');
set_time_limit(0);

	while(!feof($fp))
	{
        fgets($fp, 10240); 	
	}
	fclose($fp);
}

?>
Thanks in advance
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

That is because you output the file after your "<form>" stuff.... The code does exactly what you tell it to do...


Do you want two different files?

blah.html

Code: Select all

&lt;form action=&quote;downloads.php&quote; method=&quote;post&quote;&gt;
&lt;input type=&quote;hidden&quote; name=&quote;file&quote; value=&quote;blah.gif&quote;/&gt;
&lt;input type=&quote;submit&quote;/&gt;
&lt;/form&gt;
downloads.php

Code: Select all

<?php
if (isset($_POST['file']))
{
  set_time_limit(0);
  echo file_get_contents('./downloads/' . $_POST['file']);
}
?>
Ruski
Forum Commoner
Posts: 28
Joined: Thu May 26, 2005 3:45 am

Post by Ruski »

I dont want it to echo the file's content, i want it to download the file onto the user's computer, such as pop up a download box, that says open or save...

Thanks for your help
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

If you don't want that to happen.. You shoudn't write code that does exactly what you DO NOT want.

I gave you an example of you could change the code to something that does what you do want...

You can find another sample of some code that offers a file for download
http://timvw.madoka.be/programming/php/download.txt
Post Reply