How to download a php script directly in your script page ?

Answer : Here we create a PHP script, name it “download.php”…

How to download a php script directly in your script page ?

Here we create a PHP script, name it “download.php” and copy/paste the following code:

[pastacode lang=”php” manual=”%3C%3Fphp%0A%20%0Aignore_user_abort(true)%3B%0Aset_time_limit(0)%3B%20%2F%2F%20disable%20the%20time%20limit%20for%20this%20script%0A%20%0A%24path%20%3D%20%22%2Fabsolute_path_to_your_files%2F%22%3B%20%2F%2F%20change%20the%20path%20to%20fit%20your%20websites%20document%20structure%0A%20%0A%24dl_file%20%3D%20preg_replace(%22(%5B%5E%5Cw%5Cs%5Cd%5C-_~%2C%3B%3A%5C%5B%5C%5D%5C(%5C).%5D%7C%5B%5C.%5D%7B2%2C%7D)%22%2C%20”%2C%20%24_GET%5B’download_file’%5D)%3B%20%2F%2F%20simple%20file%20name%20validation%0A%24dl_file%20%3D%20filter_var(%24dl_file%2C%20FILTER_SANITIZE_URL)%3B%20%2F%2F%20Remove%20(more)%20invalid%20characters%0A%24fullPath%20%3D%20%24path.%24dl_file%3B%0A%20%0Aif%20(%24fd%20%3D%20fopen%20(%24fullPath%2C%20%22r%22))%20%7B%0A%20%20%20%20%24fsize%20%3D%20filesize(%24fullPath)%3B%0A%20%20%20%20%24path_parts%20%3D%20pathinfo(%24fullPath)%3B%0A%20%20%20%20%24ext%20%3D%20strtolower(%24path_parts%5B%22extension%22%5D)%3B%0A%20%20%20%20switch%20(%24ext)%20%7B%0A%20%20%20%20%20%20%20%20case%20%22pdf%22%3A%0A%20%20%20%20%20%20%20%20header(%22Content-type%3A%20application%2Fpdf%22)%3B%0A%20%20%20%20%20%20%20%20header(%22Content-Disposition%3A%20attachment%3B%20filename%3D%5C%22%22.%24path_parts%5B%22basename%22%5D.%22%5C%22%22)%3B%20%2F%2F%20use%20’attachment’%20to%20force%20a%20file%20download%0A%20%20%20%20%20%20%20%20break%3B%0A%20%20%20%20%20%20%20%20%2F%2F%20add%20more%20headers%20for%20other%20content%20types%20here%0A%20%20%20%20%20%20%20%20default%3B%0A%20%20%20%20%20%20%20%20header(%22Content-type%3A%20application%2Foctet-stream%22)%3B%0A%20%20%20%20%20%20%20%20header(%22Content-Disposition%3A%20filename%3D%5C%22%22.%24path_parts%5B%22basename%22%5D.%22%5C%22%22)%3B%0A%20%20%20%20%20%20%20%20break%3B%0A%20%20%20%20%7D%0A%20%20%20%20header(%22Content-length%3A%20%24fsize%22)%3B%0A%20%20%20%20header(%22Cache-control%3A%20private%22)%3B%20%2F%2Fuse%20this%20to%20open%20files%20directly%0A%20%20%20%20while(!feof(%24fd))%20%7B%0A%20%20%20%20%20%20%20%20%24buffer%20%3D%20fread(%24fd%2C%202048)%3B%0A%20%20%20%20%20%20%20%20echo%20%24buffer%3B%0A%20%20%20%20%7D%0A%7D%0Afclose%20(%24fd)%3B%0Aexit%3B” message=”” highlight=”” provider=”manual”/]

Create on your webpage links for each file which attacks in a password protected directory or in a directory above the website root directory.

In our example use the following download URL:

[pastacode lang=”php” manual=”%3Ca%20href%3D%22https%3A%2F%2Fmydomain.com%2Fdownload.php%3Fdownload_file%3Dsome_file.pdf%22%3EPHP%20download%20file%3C%2Fa%3E” message=”” highlight=”” provider=”manual”/]
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like