Jump to content

download script not working correctly


darkcarnival

Recommended Posts

hi,

 

im really stuck on this.

 

im working on a download script for a program of mine and i can get the file to download correctly but when i try to open it i get a bunch of random characters. now if you need to know what type of file i was opening, it was a openoffice file(.odt) but i had the MIME type and such all ther so im still not sure why its doing this.

 

here is the code im using, i hope

 

	#check if the file exists, if it doesn't, fire an error message and kill the script
$dl_path = "uploads/".$attach_r['Filename'];
if(!file_exists($dl_path)){
	$error = $attach['nofile'];
	echo error($error, "error");
}
#do some header work.
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // some browsers require this
#declares the type of file
header("Content-Type: $attach_r[File_Type]");
#declares the file as an attachment
header("Content-Disposition: attachment; filename=\"".basename($dl_path)."\";" );
#the encoding of the transfer.
header("Content-Transfer-Encoding: binary");
#the size of the file
header("Content-Length: $attach_r[File_Size]");
#to prevent script from timing out.
@set_time_limit(0);
#download file.
readfile("$dl_path");

 

the only items i didnt add in were the sql query and id check(if you wish to see that let me know).

 

much thanks :)

Link to comment
https://forums.phpfreaks.com/topic/47329-download-script-not-working-correctly/
Share on other sites

I don't think you need to set nearly as many headers for it to work just the same

 

	#check if the file exists, if it doesn't, fire an error message and kill the script
$dl_path = "uploads/".$attach_r['Filename'];
if(!file_exists($dl_path)){
	$error = $attach['nofile'];
	echo error($error, "error");
}
#do some header work.
        header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
#declares the type of file
header("Content-Type: {$attach_r['File_Type']}");
#declares the file as an attachment
header("Content-Disposition: attachment; filename=\"".basename($dl_path)."\"" );
#the size of the file
header("Content-Length: {$attach_r['File_Size']}");
#to prevent script from timing out.
@set_time_limit(0);
#download file.
readfile($dl_path);

ok well i decided to test a few things out and what i found is odd.

 

i made a similar download script that would check the file only and downloaded everything correctly.

 

but if i grab all my data from a mysql table i got errors in the files that download :?

 

now i did somewhat fix this by adding a header with a location command but if an image is in there, it'll load the image instead of downloading it.

 

any idea why it wont download correctly if the data is from a mysql table?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.