BrandonE97 Posted May 24, 2007 Share Posted May 24, 2007 I have a script that pics up a requested downloadable file but instead of the file requested the file has the name its supposed to have but the file is the html output sent to the browser. $filename = preg_replace( '@(\.\.|/)@', '', $filename ); header('Cache-Control: maxage=360'); header('Content-type: binary/octet-stream'); header('Content-Disposition:attachment; filename="'.$filename.'"'); header('Content-transfer-encoding: binary'); header('Content-length: '.(string)(filesize($filename))); readfile( $filename ); Quote Link to comment https://forums.phpfreaks.com/topic/52881-problem-with-download-script/ Share on other sites More sharing options...
corbin Posted May 24, 2007 Share Posted May 24, 2007 What type of files is this handling? And if this is in another part of a php script, you most likely want to exit; after readfile (actually you should anyway because I think it adds \n or something if you don't, corrupting the downloads). Quote Link to comment https://forums.phpfreaks.com/topic/52881-problem-with-download-script/#findComment-261114 Share on other sites More sharing options...
BrandonE97 Posted May 24, 2007 Author Share Posted May 24, 2007 Wow, would have never known about that /n, thanks. The file's are mostly mp3's and wma's of a local band. Quote Link to comment https://forums.phpfreaks.com/topic/52881-problem-with-download-script/#findComment-261117 Share on other sites More sharing options...
corbin Posted May 24, 2007 Share Posted May 24, 2007 Can I ask why "header('Content-transfer-encoding: binary');" is there? Also, can you explain what you mean by the HTML because mp3s and wma's don't have html lol... Do you mean you would see what you would see if you opened the files in notepad? If so, then your headers are incorrect. Quote Link to comment https://forums.phpfreaks.com/topic/52881-problem-with-download-script/#findComment-261119 Share on other sites More sharing options...
BrandonE97 Posted May 24, 2007 Author Share Posted May 24, 2007 This is some code I found on a google search that I've tried modding but I dont know much about headers. If the file is an mp3 then what it ask me to download is whatever.mp3 but it doesnt play. I noticed that every file was the same size of about 38kb so I opened it with notepad on an assumption and sure enough the file was the html output that the browser was seeing. Quote Link to comment https://forums.phpfreaks.com/topic/52881-problem-with-download-script/#findComment-261125 Share on other sites More sharing options...
BrandonE97 Posted May 26, 2007 Author Share Posted May 26, 2007 Okay, I've got it working except for one thing. The file name changes from something like artist name - song title.mp3 to artist_name_-_song_title.mp3. Here is my code: $filename = preg_replace("/[^.\s,a-z0-9-]/im", "", $_GET['file']); // on a script viewed by the user // the rest on a seperate script header("Cache-Control: no-cache, must-revalidate"); header('Pragma: public'); header('Pragma: hack'); header('Content-type: binary/octet-stream'); header('Content-Disposition:attachment; filename="'.$filename.'"'); header('Content-transfer-encoding: binary'); header('Content-length: '.filesize('downloads/'.$filename)); readfile('downloads/'.$filename); exit; // you also said include an exit here to keep form corrupting the files Quote Link to comment https://forums.phpfreaks.com/topic/52881-problem-with-download-script/#findComment-262214 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.