{"id":1664,"date":"2016-10-28T23:34:29","date_gmt":"2016-10-28T23:34:29","guid":{"rendered":"http:\/\/www.phpmind.com\/blog\/?p=1664"},"modified":"2016-12-28T23:14:48","modified_gmt":"2016-12-28T23:14:48","slug":"how-to-make-download-file-forcefully-and-securely","status":"publish","type":"post","link":"https:\/\/www.phpmind.com\/blog\/2016\/10\/how-to-make-download-file-forcefully-and-securely\/","title":{"rendered":"How to make  download file forcefully and securely ?"},"content":{"rendered":"<div class=\"fig\">\n<pre id=\"taag_font_Avatar\" class=\"fig \" contenteditable=\"true\">#   _____ ____  ____  ____  _____   ____  ____  _      _      _     ____  ____  ____ \r\n#  \/    \/\/  _ \\\/  __\\\/   _\\\/  __\/  \/  _ \\\/  _ \\\/ \\  \/|\/ \\  \/|\/ \\   \/  _ \\\/  _ \\\/  _ \\\r\n#  |  __\\| \/ \\||  \\\/||  \/  |  \\    | | \\|| \/ \\|| |  ||| |\\ ||| |   | \/ \\|| \/ \\|| | \\|\r\n#  | |   | \\_\/||    \/|  \\_ |  \/_   | |_\/|| \\_\/|| |\/\\||| | \\||| |_\/\\| \\_\/|| |-||| |_\/|\r\n#  \\_\/   \\____\/\\_\/\\_\\\\____\/\\____\\  \\____\/\\____\/\\_\/  \\|\\_\/  \\|\\____\/\\____\/\\_\/ \\|\\____\/\r\n#<\/pre>\n<\/div>\n<p>Warning &#8211; Be careful with this script i have modified this just for me but you can use it anyways.<\/p>\n<pre>\r\n<?php\r\ninclude_once 'Mylib\/config.php';\r\n\r\nMyClass::checkLoginRequired();\r\n\r\n\r\n\r\n$zip_files = array(\r\n\t'1_docs' => 'pass_protected\/myfile_02.zip',\r\n\t'2_docs' => 'pass_protected\/myfile_32.zip',\r\n\t'2_js' => 'pass_protected\/my_docs.zip'\r\n);\r\n\r\n\r\n$file_name = $_GET['file'];\r\n$file_name = isset($zip_files[$file_name]) ? $zip_files[$file_name] : null;\r\n$file_name = $file_name ? Config::ABS_PATH . $file_name : null;\r\n\r\nif (!$file_name) {\r\n\texit();\r\n}\r\n\r\n\r\n\r\n\/\/ make sure it's a file before doing anything!\r\nif(file_exists($file_name)) {\r\n\r\n\t\/*\r\n\t\tDo any processing you'd like here:\r\n\t\t1.  Increment a counter\r\n\t\t2.  Do something with the DB\r\n\t\t3.  Check user permissions\r\n\t\t4.  Anything you want!\r\n\t*\/\r\n\t\r\n\t\r\n\r\n\t\/\/ required for IE\r\n\tif(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off');\t}\r\n\r\n\t\/\/ get the file mime type using the file extension\r\n\tswitch(strtolower(substr(strrchr($file_name, '.'), 1))) {\r\n\t\t\/\/case 'pdf': $mime = 'application\/pdf'; break;\r\n\t\tcase 'zip': $mime = 'application\/zip'; break;\r\n\t\t\/\/case 'jpeg':\r\n\t\tcase 'jpg': $mime = 'image\/jpg'; break;\r\n\t\tdefault: $mime = 'application\/force-download';\r\n\t}\r\n\theader('Pragma: public'); \t\/\/ required\r\n\theader('Expires: 0');\t\t\/\/ no cache\r\n\theader('Cache-Control: must-revalidate, post-check=0, pre-check=0');\r\n\theader('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($file_name)).' GMT');\r\n\theader('Cache-Control: private',false);\r\n\theader('Content-Type: '.$mime);\r\n\theader('Content-Disposition: attachment; filename=\"'.basename($file_name).'\"');\r\n\theader('Content-Transfer-Encoding: binary');\r\n\theader('Content-Length: '.filesize($file_name));\t\/\/ provide file size\r\n\theader('Connection: close');\r\n\treadfile($file_name);\t\t\/\/ push it out\r\n\texit();\r\n\r\n}\r\n\r\n?>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p># _____ ____ ____ ____ _____ ____ ____ _ _ _ ____ ____ ____ # \/ \/\/ _ \\\/ __\\\/ _\\\/ __\/ \/ _ \\\/ _ \\\/ \\ \/|\/ \\ \/|\/ \\ \/ _ \\\/ _ \\\/ _ \\ # | __\\| \/ \\|| \\\/|| \/ | \\ | | \\|| \/ \\|| | ||| [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1664","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/1664","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/comments?post=1664"}],"version-history":[{"count":4,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/1664\/revisions"}],"predecessor-version":[{"id":1666,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/1664\/revisions\/1666"}],"wp:attachment":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/media?parent=1664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/categories?post=1664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/tags?post=1664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}