WeybkbcOTHER WeybkbcOTHER - 5 months ago 92
PHP Question

How to download a PHP file without executing its scripts?

I tried to download a file from another website:

I have a problem with the download, PHP scripts executes before downloading:

My code for force the download of the file that I want to download:

<?php
$f = $_GET["d"];
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$f\"\n");
$fp=fopen("$f", "r");
fpassthru($fp);
?>


My code works correctly, but during downloading, I can't find the PHP scripts:

1- I executed my PHP file typing in the browser
http://www.example.com/force_download.php?d=http://www.another_website.com/the_file.php


2- Download of
the_file.php
worked without bugs

3- Unafortunately, PHP scripts executed in the file.

Before downloading (Original file):

<html>
<body>
Example text:
<?php
$mynum = 5 + 5;
echo "5 plus 5 is" + $mynum;
?>
</body>
</html>


After downloading:

<html>
<body>
Example text:
5 plus 5 is 10

<!-- PHP code was executed during downloading -->
</body>
</html>


How to download the original PHP file without executing it (download file without alterations, like PHP script executing)?

Answer

You can't do this, PHP is a server-side language and you are trying tu pull it from client-side, PHP is always executed before it's returned

Client sends a request to the server to execute a PHP script in the server, and server returns the HTML code to client. The attempt of download is a request.

PHP is always executed on the server before return code, and for security reasons, you can't get PHP text as plain. There is not methods to get raw PHP code from server, the only way is downloading file via FTP, so you'd have to know the hostname, username and password of the www.another_website.com, If you don't know these details, you can't get the PHP file as plain text.

Comments