Jump to content

Force Download Displays Garbage


rpcob

Recommended Posts

I've tried three different attempts to force php to download. None of them are working correctly. Can anybody help me get this working? The text/garbage that displays on the screen looks like it was a zip opened in a text editor. I have also tried this http://stackoverflow...rbage-on-screen with no luck either

 

First: Returns a page full of garbage

$fid = $_GET['file'];
$results = mysql_query("SELECT filename FROM files WHERE id=$fid");
 if (mysql_numrows($results) == 0){
	 echo "<b>File not found</b>";
	 return;
 }
$file = mysql_result($results,0,"filename");
$fileurl = 'http://url.com/files/'.$file;
 // Set headers
 header("Cache-Control: public");
 header("Content-Description: File Transfer");
 header("Content-Disposition: attachment; filename=$fileurl");
 header("Content-Type: application/zip");
 header("Content-Transfer-Encoding: binary");

 // Read the file from disk
 readfile($fileurl);
exit();

 

Second: Returns same garbage (.zip file)

 

$fid = $_GET['file'];
$results = mysql_query("SELECT filename FROM files WHERE id=$fid");
 if (mysql_numrows($results) == 0){
	 echo "<b>File not found</b>";
	 return;
 }
$file = mysql_result($results,0,"filename");
$fileurl = 'http://url.com/files/'.$file;
header('Content-Description: File Transfer');
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="'.basename($fileurl).'"'); //<<< Note	 the " " surrounding the file name
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($fileurl));
readfile($fileurl);

 

Third: Returns same garbage, but also alters page layout

 

$fid = $_GET["file"];
$results = mysql_query("SELECT filename FROM files WHERE id=$fid");
 if (mysql_numrows($results) == 0){
	 echo '<b>File not found</b>';
	 return;
 }
$file = mysql_result($results,0,'filename');
$fileurl = "http://url.com/files/".$file;
// Inform browser that this is a force-download
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
// Inform browser that data can be binary in addition to text
header('Content-Disposition: attachment; filename='.basename($fileurl));
header('Content-Transfer-Encoding: binary');
// Inform browser that this page expires immediately so that an update to the file will still work.
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($fileurl));
// Push actual file.
ob_clean();
flush();
readfile($fileurl);
exit();

 

Any help, insight, direction, or links would be greatly appreciated. Thank you.

On a side note there are .zip, .rar, and .txt files. Do i just add content type lines?

Link to comment
Share on other sites

also alters page layout

 

^^^ That would imply that you are trying to put the posted code onto a web page. You cannot output anything on a web page besides the html/css/javascript. Any force-download/dynamic image must be output as a completely separate response by putting a link to the force-download .php code into your html markup on a web page.

 

What's the actual code on the page that doesn't work? Also, how exactly did you turn on error_reporting/display_errors and did you confirm that they actually got changed by intentionally producing an error?

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.