file download script does not work

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
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

file download script does not work

Post by raghavan20 »

can anybody help me with this script?

Code: Select all

if (isset($_GET["file"])){
	$size = filesize($_GET["file"]);
	$file = $_GET["file"];
	header("Content-type: text/plain"); 
	header("Content-Length: $size");
	header("Content-Disposition: attachment; filename=$file");
	include($file);
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

don't use include(), readfile() is often best for this. Try placing double quotes around the filename in the disposition line.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

Code: Select all

<?
header("Content-type: text/plain"); //here you will have to mention the file type 
header("Content-Length: 1024");     // this is the filesize 
header("Content-Disposition: attachment; filename=test.php"); // filename is the name you want to store the file 
//as.It can also be any other name 

include 'test.php'; // this is the file that you want to download  
?>
this is a static script which works fine....then why should not the earlier one work???
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

that wouldn't work fine if any of the contents of test.php are in fact php. ;)
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

When I used the static script to download the test.php, all the php code has been cut off and the html code was intact.
http://raghavan.100webcustomers.com/download.php

why it can't read the php content? what do I have to do with content type to download a php file???

first of all, wots the logic behind this code.... :?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

as I said before, use readfile(). Include() asks php to parse the file. readfile() opens and outputs the file to the client, no processing via php.
Post Reply