Page 1 of 1

Secure Downloading Problem

Posted: Sun Jun 19, 2005 5:32 am
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

Posted: Sun Jun 19, 2005 6:44 am
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']);
}
?>

Posted: Sun Jun 19, 2005 7:21 am
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

Posted: Sun Jun 19, 2005 7:49 am
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