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

时间:2013-09-05 14:42:33

标签: php download

我用php创建了一个文件下载系统。我创造了那样的

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; 
} ?>

我还创建了testing.php文件,如下所示

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

当我从本地主机运行phpfiledownload.php时,我收到了testing.php个文件。

但是当我在testing.php中将http://www.anotherdomain.com/example.php更改为phpfiledownload.php时,我无法下载http://www.anotherdomain.com/example.php

那么,我如何通过我的phpfiledownload.php获取http://www.anotherdomain.com/example.php

3 个答案:

答案 0 :(得分:0)

你的问题不是很清楚:你是想下载php文件还是web服务器处理的结果?

在您的代码中,“testing.php”是本地文件,而“http://www.example.com/example.php”是一个网址。

在第一种情况下,您的本地Web服务器将获取本地文件并使用相应的标头返回它。

在第二种情况下,您只获得网站“http://www.example.com”的网络服务器生成的html输出

答案 1 :(得分:0)

  

提示

     

如果已启用fopen wrappers,则可以将URL用作具有此功能的文件名。有关如何操作的详细信息,请参阅fopen()   指定文件名。请参阅Supported Protocols and Wrappers   链接到有关各种包装器具有哪些功能的信息,   关于它们的使用和它们的任何预定义变量的信息的注释   可能会提供。

取自PHP手册,here

答案 2 :(得分:0)

要下载http://www.example.com/example.php,您可以使用以下代码

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

注意:如果exam​​ple.php包含php代码,它将在Web服务器上运行并将HTML输出返回给您。所以你的文件是example.php的输出而不是源代码。