如何使用fread(或readfile)函数(PHP)来获取另一个PHP页面的输出结果?

时间:2015-12-18 08:18:52

标签: php readfile fread

摘要

我正在尝试使用fread(或readfile)函数(PHP)来获取另一个PHP页面的输出结果。例如,我想读取(或者甚至作为另一个文件下载到我的PC)页面“add.php?a = 1& b = 4”的输出结果。以下是PHP代码。

代码

read.php

<?php
$filename = "add.php?a=1&b=4";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
echo $contents;        //I hope it can output “5” (the output result of the page “add.php?a=1&b=4”)
fclose($handle);
?>

add.php

<?php
echo $_GET["a"]+$_GET["b"];
?>

以下是readfile功能的示例。我想下载页面“add.php?a = 1&amp; b = 4”的输出结果作为文件&#34; result.txt&#34;。但它也不起作用。

readfile.php

<?php
header("Content-type:application");
header("Content-Disposition: attachment; filename=result.txt"); 
readfile("add.php?a=1&b=4");
?>

我该如何修改?

2 个答案:

答案 0 :(得分:2)

如果要阅读Web输出,则需要通过HTTP服务器读取它。

<embed/>

答案 1 :(得分:0)

$url = "[absolute url]/add.php?a=1&b=4";

header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"result.txt\""); 

readfile($url);