Outputting a file

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
Ralle
Forum Commoner
Posts: 38
Joined: Mon Oct 17, 2005 5:05 am

Outputting a file

Post 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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the error is coming from your last header call. Not specifically the header call, but the use of $filename() (remove the parentheses)
Ralle
Forum Commoner
Posts: 38
Joined: Mon Oct 17, 2005 5:05 am

Post by Ralle »

now both scripts outputs the same:
downloadmap
no type, no real filename..
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Your echo's may have stopped php from letting the headers actually get sent
Ralle
Forum Commoner
Posts: 38
Joined: Mon Oct 17, 2005 5:05 am

Post by Ralle »

nope.. Both still doesn't work try this: http://www.hiveworkshop.com/forum/downloadmap.php?id=25
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Ralle
Forum Commoner
Posts: 38
Joined: Mon Oct 17, 2005 5:05 am

Post by Ralle »

[quote]HM3WBooty BayœMPQ ÍäÍßÍã@ˆPû ˜)N`!x'1.ý4Ã;{BVIbPÇWð^•eÇkWqñv}xƒ<Šê
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Did you remove the content-type header?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Ralle
Forum Commoner
Posts: 38
Joined: Mon Oct 17, 2005 5:05 am

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Ralle
Forum Commoner
Posts: 38
Joined: Mon Oct 17, 2005 5:05 am

Post by Ralle »

arg what when I upload it to my host??? how would I know it's path???
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Lose this line:

Code: Select all

Header('Content-Type: application/x-octet-stream');
Ralle
Forum Commoner
Posts: 38
Joined: Mon Oct 17, 2005 5:05 am

Post 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..
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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));
Post Reply