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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.