rpcob Posted December 11, 2012 Share Posted December 11, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/271844-force-download-displays-garbage/ Share on other sites More sharing options...
Christian F. Posted December 11, 2012 Share Posted December 11, 2012 Do you get any error messages? Try commenting out the content-type header, and see. Quote Link to comment https://forums.phpfreaks.com/topic/271844-force-download-displays-garbage/#findComment-1398708 Share on other sites More sharing options...
rpcob Posted December 12, 2012 Author Share Posted December 12, 2012 i turned on error report all and removed the content-type header. Made no difference Quote Link to comment https://forums.phpfreaks.com/topic/271844-force-download-displays-garbage/#findComment-1398815 Share on other sites More sharing options...
PFMaBiSmAd Posted December 12, 2012 Share Posted December 12, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/271844-force-download-displays-garbage/#findComment-1398817 Share on other sites More sharing options...
rpcob Posted December 12, 2012 Author Share Posted December 12, 2012 @ PFMaBiSmAd that's what i was thinking, but i was hoping to get around that. The url is http://url.com/download/?nav=display&file=12. The download link on the page send you to http://url.com/download/?nav=get&file=12. So I need to break the nav.php and display.php? Quote Link to comment https://forums.phpfreaks.com/topic/271844-force-download-displays-garbage/#findComment-1398819 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.