Jump to content

Problem with download script


BrandonE97

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/52881-problem-with-download-script/
Share on other sites

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.

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.

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

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.