<br />
<b>Warning</b>:  Undefined array key "global_protection_id" in <b>/home/wikitechy/public_html/interview-questions/wp-content/plugins/content-protector/inc/class-ps-rest-handler.php</b> on line <b>51</b><br />
{"id":1146,"date":"2021-07-21T13:20:09","date_gmt":"2021-07-21T13:20:09","guid":{"rendered":"https:\/\/www.wikitechy.com\/interview-questions\/?p=1146"},"modified":"2021-09-08T11:38:53","modified_gmt":"2021-09-08T11:38:53","slug":"how-to-download-a-php-script-directly-in-your-script-page","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-download-a-php-script-directly-in-your-script-page\/","title":{"rendered":"How to download a php script directly in your script page ?"},"content":{"rendered":"<div class=\"TextHeading\">\n<div class=\"hddn\">\n<h2 id=\"how-to-download-a-php-script-directly-in-your-script-page\" class=\"color-pink\">How to download a php script directly in your script page ?<\/h2>\n<\/div>\n<\/div>\n<p>Here we create a PHP script, name it \u201cdownload.php\u201d and copy\/paste the following code:<\/p>\n<div class=\"text-center row\">\n<div class=\"col-sm-12\">\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">&lt;?php<br\/> <br\/>ignore_user_abort(true);<br\/>set_time_limit(0); \/\/ disable the time limit for this script<br\/> <br\/>$path = &quot;\/absolute_path_to_your_files\/&quot;; \/\/ change the path to fit your websites document structure<br\/> <br\/>$dl_file = preg_replace(&quot;([^\\w\\s\\d\\-_~,;:\\[\\]\\(\\).]|[\\.]{2,})&quot;, &#039;&#039;, $_GET[&#039;download_file&#039;]); \/\/ simple file name validation<br\/>$dl_file = filter_var($dl_file, FILTER_SANITIZE_URL); \/\/ Remove (more) invalid characters<br\/>$fullPath = $path.$dl_file;<br\/> <br\/>if ($fd = fopen ($fullPath, &quot;r&quot;)) {<br\/>    $fsize = filesize($fullPath);<br\/>    $path_parts = pathinfo($fullPath);<br\/>    $ext = strtolower($path_parts[&quot;extension&quot;]);<br\/>    switch ($ext) {<br\/>        case &quot;pdf&quot;:<br\/>        header(&quot;Content-type: application\/pdf&quot;);<br\/>        header(&quot;Content-Disposition: attachment; filename=\\&quot;&quot;.$path_parts[&quot;basename&quot;].&quot;\\&quot;&quot;); \/\/ use &#039;attachment&#039; to force a file download<br\/>        break;<br\/>        \/\/ add more headers for other content types here<br\/>        default;<br\/>        header(&quot;Content-type: application\/octet-stream&quot;);<br\/>        header(&quot;Content-Disposition: filename=\\&quot;&quot;.$path_parts[&quot;basename&quot;].&quot;\\&quot;&quot;);<br\/>        break;<br\/>    }<br\/>    header(&quot;Content-length: $fsize&quot;);<br\/>    header(&quot;Cache-control: private&quot;); \/\/use this to open files directly<br\/>    while(!feof($fd)) {<br\/>        $buffer = fread($fd, 2048);<br\/>        echo $buffer;<br\/>    }<br\/>}<br\/>fclose ($fd);<br\/>exit;<\/code><\/pre> <\/div>\n<\/div>\n<\/div>\n<p>Create on your webpage links for each file which attacks in a password protected directory or in a directory above the website root directory.<\/p>\n<p>In our example use the following download URL:<\/p>\n<div class=\"CodeContent\">\n<div class=\"hddn\">\n<figure class=\"highlight\"><div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">&lt;a href=&quot;https:\/\/mydomain.com\/download.php?download_file=some_file.pdf&quot;&gt;PHP download file&lt;\/a&gt;<\/code><\/pre> <\/div><\/figure>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Answer : Here we create a PHP script, name it &#8220;download.php&#8221;&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"passster_activate_protection":false,"passster_protect_child_pages":"","passster_protection_type":"password","passster_password":"","passster_activate_overwrite_defaults":"","passster_headline":"","passster_instruction":"","passster_placeholder":"","passster_button":"","passster_id":"","passster_activate_misc_settings":"","passster_redirect_url":"","passster_hide":"no","passster_area_shortcode":"","gtb_hide_title":false,"gtb_wrap_title":false,"gtb_class_title":"","gtb_remove_headerfooter":false,"footnotes":""},"categories":[8182],"tags":[5432,8217,195,8293,8303,491,7383,7352,199,214,198,8295,5940,290,205,8280,8283,8300,8301,8312,8216,8288,8289,8316,8296,8291,8292,196,212,7677,5941,215,207,8297,8214,204,8276,8273,8277,8278,8275,8284,8304,8302,8299,8279,8274,8287,8317,8281,8282,8298,8305,8309,8290,8315,8310,8314,8294,8286,208,1744,8306,8313,8308,8215,200,8311,8307,197,284,280,216,8285,15852],"class_list":["post-1146","post","type-post","status-publish","format-standard","hentry","category-php","tag-3i-infotech-limited-interview-questions-and-answers","tag-accel-frontline-ltd-interview-questions-and-answers","tag-accenture-interview-questions-and-answers","tag-add-php-script-to-html","tag-apache-php-download-instead-execute","tag-applied-materials-interview-questions-and-answers","tag-brillio-technologies-pvt-ltd-interview-questions-and-answers","tag-cactus-communications-interview-questions-and-answers","tag-casting-networks-india-pvt-limited-interview-questions-and-answers","tag-cgi-group-inc-interview-questions-and-answers","tag-chetu-interview-questions-and-answers","tag-compile-php-script","tag-csg-international-interview-questions-and-answers","tag-cybage-interview-questions-and-answers","tag-dell-international-services-india-pvt-ltd-interview-questions-and-answers","tag-download-a-folder-using-phpdownload-php-file-from-server-directory","tag-download-file-from-folder-in-php","tag-download-file-php-script","tag-download-php-files","tag-execute-php-script","tag-harbinger-systems-interview-questions-and-answers","tag-how-to-call-php-script-from-html","tag-how-to-create-php-script","tag-how-to-execute-php-script","tag-how-to-make-a-php-script","tag-how-to-use-php-script-in-html","tag-how-to-use-script-in-php","tag-ibm-interview-questions-and-answers","tag-indecomm-global-services-interview-questions-and-answers","tag-info-edge-interview-questions-and-answers","tag-infogain-interview-questions-and-answers","tag-maintec-technologies-pvt-ltd-interview-questions-and-answers","tag-mphasis-interview-questions-and-answers","tag-mysql-trigger-php-script","tag-net-solutions-interview-questions-and-answers","tag-oracle-corporation-interview-questions-and-answers","tag-php-code-for-downloading-a-file-from-database","tag-php-code-for-downloading-a-file-from-folder","tag-php-code-for-downloading-a-file-from-server","tag-php-download-all-files-in-directory","tag-php-download-file-script","tag-php-download-folder-from-servercode-for-downloading-a-file-in-phphow-to-make-a-file-downloadable-in-phpphp-script-free-download","tag-php-download-php-file","tag-php-file-download-example","tag-php-file-download-script","tag-php-file-download-script-example","tag-php-file-downloader","tag-php-file-hosting-script","tag-php-include-script","tag-php-list-all-files-in-directory-and-subdirectories","tag-php-list-folders-in-directory","tag-php-mysql-download","tag-php-script","tag-php-script-example","tag-php-script-in-html-page","tag-php-script-name","tag-php-script-online","tag-php-script-path","tag-php-server-script","tag-php-web-script","tag-prokarma-softech-pvt-ltd-interview-questions-and-answers","tag-raqmiyat-information-technologies-pvt-ltd-interview-questions-and-answers","tag-run-php-script","tag-run-php-script-from-command-line","tag-sample-php-script","tag-sopra-steria-interview-questions-and-answers","tag-tech-mahindra-interview-questions-and-answers","tag-test-php-script","tag-test-php-script-online","tag-unitedhealth-group-interview-questions-and-answers","tag-valuelabs-interview-questions-and-answers","tag-virtusa-consulting-services-pvt-ltd-interview-questions-and-answers","tag-wipro-interview-questions-and-answers","tag-write-php-script","tag-zmanda-interview-questions-and-answers"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to download a php script directly in your script page ? - PHP<\/title>\n<meta name=\"description\" content=\"How to download a php script directly in your script page ? - PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-download-a-php-script-directly-in-your-script-page\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to download a php script directly in your script page ? - PHP\" \/>\n<meta property=\"og:description\" content=\"How to download a php script directly in your script page ? - PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-download-a-php-script-directly-in-your-script-page\/\" \/>\n<meta property=\"og:site_name\" content=\"Wikitechy\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-21T13:20:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-09-08T11:38:53+00:00\" \/>\n<meta name=\"author\" content=\"Editor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Editor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-download-a-php-script-directly-in-your-script-page\/\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-download-a-php-script-directly-in-your-script-page\/\",\"name\":\"How to download a php script directly in your script page ? - PHP\",\"isPartOf\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\"},\"datePublished\":\"2021-07-21T13:20:09+00:00\",\"dateModified\":\"2021-09-08T11:38:53+00:00\",\"author\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757\"},\"description\":\"How to download a php script directly in your script page ? - PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-download-a-php-script-directly-in-your-script-page\/\"]}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/\",\"name\":\"Wikitechy\",\"description\":\"Interview Questions\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757\",\"name\":\"Editor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g\",\"caption\":\"Editor\"},\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/author\/editor\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to download a php script directly in your script page ? - PHP","description":"How to download a php script directly in your script page ? - PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-download-a-php-script-directly-in-your-script-page\/","og_locale":"en_US","og_type":"article","og_title":"How to download a php script directly in your script page ? - PHP","og_description":"How to download a php script directly in your script page ? - PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages","og_url":"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-download-a-php-script-directly-in-your-script-page\/","og_site_name":"Wikitechy","article_published_time":"2021-07-21T13:20:09+00:00","article_modified_time":"2021-09-08T11:38:53+00:00","author":"Editor","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Editor","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-download-a-php-script-directly-in-your-script-page\/","url":"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-download-a-php-script-directly-in-your-script-page\/","name":"How to download a php script directly in your script page ? - PHP","isPartOf":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website"},"datePublished":"2021-07-21T13:20:09+00:00","dateModified":"2021-09-08T11:38:53+00:00","author":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757"},"description":"How to download a php script directly in your script page ? - PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-download-a-php-script-directly-in-your-script-page\/"]}]},{"@type":"WebSite","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website","url":"https:\/\/www.wikitechy.com\/interview-questions\/","name":"Wikitechy","description":"Interview Questions","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757","name":"Editor","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g","caption":"Editor"},"url":"https:\/\/www.wikitechy.com\/interview-questions\/author\/editor\/"}]}},"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/1146","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/comments?post=1146"}],"version-history":[{"count":2,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/1146\/revisions"}],"predecessor-version":[{"id":3041,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/1146\/revisions\/3041"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/media?parent=1146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/categories?post=1146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/tags?post=1146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}