Mitchey-Welch Posted August 27, 2012 Share Posted August 27, 2012 Hello, Recently I have been coding a system for an upload/sharing file site of mine. However I have come across an issue that I have tried to resolve, but can't. This is the downloadnow.php (The tab closes instantly, but the download correctly starts.) <?PHP include_once('global.php'); $id = mysql_real_escape_string($_GET['id']); $query = mysql_query("SELECT * FROM uploads WHERE uniq_id = '" . $id . "'"); $mainrow = mysql_fetch_array($query); $dir = "uploads"; $file = $dir . "/" . $mainrow['file_name']; $filename = $dir . "/" . $mainrow['file_name']; if(file_exists($file)) { header('Content-Description: File Transfer'); header('Content-Disposition: attachment; filename=' . basename($file)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); ob_clean(); flush(); readfile($file); }else{ die("I'm sorry, this file either does not exist or has been deleted by the user."); } ?> <script> window.close(); </script> However, if I visit downloadnow.php WITHOUT an ID, such as downloadnow.php?id=612j2dh71, then it downloads the directory.htm, in this case uploads.htm. It's really frustrating, and I hope someone can help me out! Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/267646-php-forced-download-not-working-correctly/ Share on other sites More sharing options...
PeoMachine Posted August 27, 2012 Share Posted August 27, 2012 Try to send a header with "Content-type"... something like: header('Content-type: application/pdf'); Quote Link to comment https://forums.phpfreaks.com/topic/267646-php-forced-download-not-working-correctly/#findComment-1372800 Share on other sites More sharing options...
Mitchey-Welch Posted August 27, 2012 Author Share Posted August 27, 2012 But it isn't necessarily a type, it's any format. So it could be JPG, PNG, EXE, GIF, PDF, anything. Quote Link to comment https://forums.phpfreaks.com/topic/267646-php-forced-download-not-working-correctly/#findComment-1372807 Share on other sites More sharing options...
Christian F. Posted August 27, 2012 Share Posted August 27, 2012 The most common MIME type to send for forced downloads is "application/octet-stream". Quote Link to comment https://forums.phpfreaks.com/topic/267646-php-forced-download-not-working-correctly/#findComment-1372813 Share on other sites More sharing options...
Mitchey-Welch Posted August 27, 2012 Author Share Posted August 27, 2012 That has just made it instead of .htm, it's made it just the filename uploads with no extension. In the file, it shows the <script> window.close(); </script> which is placed after the PHP.. Should I put the script to close the window into the PHP? Also, when I hit the download button on my site it actually downloads both now? Quote Link to comment https://forums.phpfreaks.com/topic/267646-php-forced-download-not-working-correctly/#findComment-1372820 Share on other sites More sharing options...
Mitchey-Welch Posted August 27, 2012 Author Share Posted August 27, 2012 <?php header("content-type: application/x-javascript"); echo "window.close();"; ?> Tried that, still downloads and in the uploads file that it downloads, its now only the window.close(); Any ideas?! Quote Link to comment https://forums.phpfreaks.com/topic/267646-php-forced-download-not-working-correctly/#findComment-1372830 Share on other sites More sharing options...
Mitchey-Welch Posted August 27, 2012 Author Share Posted August 27, 2012 Now I'm confused.. It sometimes downloads the upload with the file, and sometimes does not.. Why does it download uploads.htm? Quote Link to comment https://forums.phpfreaks.com/topic/267646-php-forced-download-not-working-correctly/#findComment-1372888 Share on other sites More sharing options...
Christian F. Posted August 27, 2012 Share Posted August 27, 2012 When making a download manager script, you need to only send the headers and data for the file itself. No other content should be sent to the browser, or you will have problems. The browser itself will take care of closing the "window", if it notices that it's only a downloadable file. If not, then its on your browser and not your code. Quote Link to comment https://forums.phpfreaks.com/topic/267646-php-forced-download-not-working-correctly/#findComment-1373017 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.