我可以从http://www.example.com/example.php下载.php文件吗?

I have created a file download system with php. I created like that

phpfiledownload.php
--------------------

<?php 
$file = 'testing.php'; 
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));
  ob_clean();
  flush();
  readfile($file);
  exit; 
} ?>

And I also created testing.php file like the following

testing.php
------------
<?php echo "Hello World"; ?>

When I run phpfiledownload.php form my localhost I got testing.php file.

But when I change testing.php to http://www.anotherdomain.com/example.php in phpfiledownload.php I can't download http://www.anotherdomain.com/example.php.

So, how I can got http://www.anotherdomain.com/example.php via my phpfiledownload.php

To download http://www.example.com/example.php you can use the code below

file_put_contents("example.php", fopen("http://www.example.com/example.php", 'r'));

Note: If example.php contains php code it will run on the webserver and return the HTML output to you. So your file be the output of example.php and not the source code.

Your question is not very clear: are you meaning to download the php file or the result processed by the web server?

In your code, "testing.php" is a local file, while "http://www.example.com/example.php" is an URL.

In the first case your local web server fetches the local file and return it using the appropriate headers.

In the second case you obtain only the html output produced by the web server of the site "http://www.example.com"

Tip

A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename. See the Supported Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.

Taken from PHP Manual, here.