Ninjakreborn Posted November 8, 2010 Share Posted November 8, 2010 Is there a way (in PHP) to prompt to download file regardless of it's type? I have taken someone's suggestions and put files in a folder with HTAccess protection. ALL I want to do is just grab the files and prompt download (regardless of what the type of file is), is there an easy way to do this in PHP. Quote Link to comment https://forums.phpfreaks.com/topic/218117-file-downloads/ Share on other sites More sharing options...
Ninjakreborn Posted November 8, 2010 Author Share Posted November 8, 2010 Here is what I have created so far. This does what I want, but screws up the file. <?php require_once('config.php'); $file_id = mysql_real_escape_string($_REQUEST['file_id']); $sql = "SELECT * FROM order_files WHERE file_id = '" . $file_id . "'"; $query = mysql_query($sql); header("Content-Type: application/force-download"); while ($row = mysql_fetch_array($query, MYSQL_ASSOC)) { $file = '../' . $row['file_name']; $filename = explode('/', $file); $filename = $filename[2]; header( "Content-Disposition: attachment; filename=../" . $filename); } echo $file; if (file_exists($file)) { readfile($file); } ?> It does prompt download no matter what the filetype is. The only bad thing is, it messes up the file. So if it's an image file, it won't let me open that image file. If it was a PDF it corrupts it. I don't want to have to do a file detection, because I am not sure what all types of files it might be. It could be virtually any type of file. Without having to analyze every possible extension. Quote Link to comment https://forums.phpfreaks.com/topic/218117-file-downloads/#findComment-1131818 Share on other sites More sharing options...
AbraCadaver Posted November 8, 2010 Share Posted November 8, 2010 Try something like this: $file = '/path/to/your/file.pdf'; header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.basename($file)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); readfile($file); Quote Link to comment https://forums.phpfreaks.com/topic/218117-file-downloads/#findComment-1131819 Share on other sites More sharing options...
Ninjakreborn Posted November 8, 2010 Author Share Posted November 8, 2010 Wow, very nice. I took what you posted and adapted it to my code. It works great. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/218117-file-downloads/#findComment-1131820 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.