Downloading a file via php

Soldato
Joined
4 Jul 2004
Posts
2,647
Location
aberdeen
Hello
I have a file called download.php, and it basically checks if a user is logged in, if they are it uses fopen/fread to open + send a file, but because the file name is .php it saves it as download.php. Is there any way to make it save it as a different name, without using htaccess?

Code:
<? 

include("include/session.php");
// checks if user is logged in
if($session->logged_in){
  //sets the header to appluication/zip cos its a zip file
header('Content-type: application/zip');

$fp = fopen('examplefile.zip', 'rb');
while (!feof($fp))
echo fread($fp, 1024);
fclose($fp);

}
// else if not logged in
else{
//redirect to the main page (yeh i should put a sorry you cant download it yet, but meh
		  header("Location: main.php");
		  
}
?>
 
I think he's trying to make the change the name of the file he's presenting to the user, he's already got the rest sorted (although readfile would indeed be better). You'll have to play around with headers and MIME-types I think, although I don't know a huge amount about this (unless you want to simply redirect to the file).
 
default to application/octet-stream and the common browsers will decide mime-type, for anyone using a browser that doesn't... they'll have to wait until they have finished downloading and the OS will decide.
 
Back
Top Bottom