Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download a php file without executing it?

Tags:

php

download

im working on a content management system for that i have to download a php file using php code without executing. any one can help me on this

it is some thing like ftp. i have added the options to upload, edit and download a file. it is working fine. but while downloading a php file it is executed instead of downloading...

What i tried is:

<?php
$file = $_REQUEST['file_name'];

if (file_exists($file)) {
    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));

    include_once($file);
    exit;
}
?>
like image 558
Jagadeesan Avatar asked Jun 12 '11 09:06

Jagadeesan


People also ask

How do I make my PHP file downloadable?

Generally, no PHP script is required to download a file with the extensions exe and zip. If the file location of this type of file is set in the href attribute of the anchor element, then the file automatically downloads when the user clicks on the download link.

How can I download PHP code from any website?

How do I get the PHP code from a website? Unless it is given to you or made available to you in some manner, you cannot get the PHP code from the web server. PHP is a “server-side language” meaning it generates HTML of the web page you view, so it is a “step removed” from you when you view a web page.

Do PHP files need to be executable?

Note that the file does not need to be executable or special in any way. The server finds out that this file needs to be interpreted by PHP because you used the ". php" extension, which the server is configured to pass on to PHP.

Can PHP be used offline?

In first type you can install and configure apache php and mysql one by one manually Or in second type use easy to install local server sotwares like xampp, wamp, easyPHP ect. By using any one method your pc will run as a server and you can easily test your php codes without any internet connectivity.


1 Answers

You have to load the files content, write the content to the request and set the headers so that it's parsed as force download or octet stream.

For example:

http://server.com/download.php?name=test.php

Contents of download.php:

  <?php 
  $filename = $_GET["name"]; //Obviously needs validation
  ob_end_clean();
  header("Content-Type: application/octet-stream; "); 
  header("Content-Transfer-Encoding: binary"); 
  header("Content-Length: ". filesize($filename).";"); 
  header("Content-disposition: attachment; filename=" . $filename);
  readfile($filename);
  die();
  ?>

This code works without any modification. Although it needs validation and some security features.

like image 117
rzetterberg Avatar answered Oct 01 '22 06:10

rzetterberg