darkcarnival Posted April 17, 2007 Share Posted April 17, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/47329-download-script-not-working-correctly/ Share on other sites More sharing options...
Glyde Posted April 17, 2007 Share Posted April 17, 2007 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); Quote Link to comment https://forums.phpfreaks.com/topic/47329-download-script-not-working-correctly/#findComment-230881 Share on other sites More sharing options...
darkcarnival Posted April 17, 2007 Author Share Posted April 17, 2007 nope still get the random characters. now i did do a direct link to make sure it wasnt an error during the upload and that downloads just fine. I am going to try another file type and see if thats whats causing this. Quote Link to comment https://forums.phpfreaks.com/topic/47329-download-script-not-working-correctly/#findComment-230896 Share on other sites More sharing options...
darkcarnival Posted April 17, 2007 Author Share Posted April 17, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/47329-download-script-not-working-correctly/#findComment-231660 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.