Page 1 of 1

Outputting a file

Posted: Sun Oct 30, 2005 2:47 pm
by Ralle
I want to make a script (like I've seen many people trying to make but which doesn't help me) thats like you write downloadmap.php?id=20 and it starts sending you map 20 which filename is pulled out of the db.
this is a script that didn't work:

Code: Select all

this is my script so far: 
PHP: 
<? 
// standard hack prevent 
define('IN_PHPBB', true); 
$phpbb_root_path = './'; 
include($phpbb_root_path . 'extension.inc'); 
include($phpbb_root_path . 'common.'.$phpEx); 

$id = $_GET['id']; 
$result = mysql_query("SELECT * FROM maps WHERE maps_id='$id'"); 
while ($row = mysql_fetch_array($result)) 

$filename = 'maps/' . $row['maps_file']; 

$file = fopen($filename, 'r'); 

//set some HTTP headers 
Header('Content-Type: application/x-octet-stream'); 
Header('Content-Transfer-Encoding: binary'); 
Header('Content-Length: ' . filesize($filename)); 
Header('Cache-Control: no-cache, must-revalidate'); //HTTP 1.1 
Header('Cache-Control: post-check=0, pre-check=0', false); //HTTP 1.1 
Header('Pragma: no-cache'); //HTTP 1.0 
Header('Content-Description: Whatever the file is'); 
Header('Content-Disposition: attachment; filename="'.$filename.'"'); 
Header('Title: ' .$filename()); 

while(!$feof($file)) 
     print(fread($file, 4096)); 

fclose($file); 

?>
It errors:
Fatal error: Call to undefined function: maps/(6)dragonfire.w3m() in /home/hivework/public_html/forum/downloadmap.php on line 25
So I tried to get one script from another site:

Code: Select all

<?
// standard hack prevent 
define('IN_PHPBB', true);
$phpbb_root_path = './';
include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx);

$id = $_GET['id'];
$result = mysql_query("SELECT * FROM maps WHERE maps_id='$id'");
while ($row = mysql_fetch_array($result))

//echo "$filename,$size,$filetype";
$filepath = 'maps/' . $row['maps_file'];
$filename = $row['maps_file'];
$size = filesize($filename);
$type = filetype($filename);
echo $filename;
echo $size;
echo $type;
///*
header("Content-Disposition: attachment; filename=$filename"); 
header("Content-length: $size");
header("Content-type: $type"); 

readfile($filepath); 
//*/
?>
This outputs a file called downloadmap
it has no type but the size is correct. NOTE: downloadmap is the name of the php script.[/list]

Posted: Sun Oct 30, 2005 2:59 pm
by feyd
the error is coming from your last header call. Not specifically the header call, but the use of $filename() (remove the parentheses)

Posted: Sun Oct 30, 2005 3:04 pm
by Ralle
now both scripts outputs the same:
downloadmap
no type, no real filename..

Posted: Sun Oct 30, 2005 3:53 pm
by feyd
Your echo's may have stopped php from letting the headers actually get sent

Posted: Mon Oct 31, 2005 11:29 am
by Ralle
nope.. Both still doesn't work try this: http://www.hiveworkshop.com/forum/downloadmap.php?id=25

Posted: Mon Oct 31, 2005 12:12 pm
by pickle
This is what I use:

Code: Select all

//define the file we're working on
$file = '/path/to/$filename';

//get the mime type of the file
$mime_type = shell_exec("file -bi $file");

//send the mime type
header("Content-type: $mime_type");

//send the contents of the file
readfile($file);
If you want to force the download, rather than potentially have the browser run it (JPG, PHP, etc fils), I think you can send the Header('Content-Disposition: attachment; filename="'.$filename.'"'); header instead of the Content-type header and the browser will automatically download it.

Posted: Mon Oct 31, 2005 12:26 pm
by Ralle
[quote]HM3WBooty BayœMPQ ÍäÍßÍã@ˆPû ˜)N`!x'1.ý4Ã;{BVIbPÇWð^•eÇkWqñv}xƒ<Šê

Posted: Mon Oct 31, 2005 12:33 pm
by pickle
Did you remove the content-type header?

Posted: Mon Oct 31, 2005 12:56 pm
by Ralle

Code: Select all

<? 
// standard hack prevent 
define('IN_PHPBB', true); 
$phpbb_root_path = './'; 
include($phpbb_root_path . 'extension.inc'); 
include($phpbb_root_path . 'common.'.$phpEx); 

$id = $_GET['id']; 
$result = mysql_query("SELECT * FROM maps WHERE maps_id='$id'"); 
while ($row = mysql_fetch_array($result)) 
{
$file = 'maps/' . $row['maps_file']; 
}
//get the mime type of the file 
$mime_type = shell_exec("file -bi $file"); 

//send the mime type 
header("Content-type: $mime_type"); 

//send the contents of the file 
readfile($file); 
?>
Nope... It just outputs craptext

Posted: Mon Oct 31, 2005 1:05 pm
by pickle
You're probably having trouble with the file path. Make sure the $file variable contains the full path of the file right from the root.

Posted: Mon Oct 31, 2005 1:09 pm
by Ralle
arg what when I upload it to my host??? how would I know it's path???

Posted: Mon Oct 31, 2005 1:50 pm
by pickle
Do you know where your 'maps' directory is on the server? If not, you can call getcwd() to find out where you are. You can hardcode the path into the $file variable just like you've already done with 'maps/'.

If you want to force a download, you should replace the content-type header with the content-disposition: attachment header you had earlier.

Posted: Mon Oct 31, 2005 2:05 pm
by Jenk
Lose this line:

Code: Select all

Header('Content-Type: application/x-octet-stream');

Posted: Tue Nov 01, 2005 1:24 am
by Ralle

Code: Select all

<? 
// standard hack prevent 
define('IN_PHPBB', true); 
$phpbb_root_path = './'; 
include($phpbb_root_path . 'extension.inc'); 
include($phpbb_root_path . 'common.'.$phpEx); 

$id = $_GET['id']; 
$result = mysql_query("SELECT * FROM maps WHERE maps_id='$id'"); 
while ($row = mysql_fetch_array($result)) 
{
$file = 'maps/' . $row['maps_file']; 
}
//get the mime type of the file 
$mime_type = shell_exec("file -bi $file"); 

//send the mime type 
header('Content-type: application/x-octet-stream'); 

//send the contents of the file 
readfile($file); 
?>
Also outputs crap..

Posted: Tue Nov 01, 2005 2:09 am
by Jenk
Jenk wrote:Lose this line:

Code: Select all

Header('Content-Type: application/x-octet-stream');
and use this line:

Code: Select all

header('Content-Disposition: attachment; filename=' . basename($file));