Jump to content

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

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.