当前位置:Gxlcms > PHP教程 > 为何无法将web页面写入指定的文件?

为何无法将web页面写入指定的文件?

时间:2021-07-01 10:21:17 帮助过:9人阅读

function downfile($fileurl)
{
ob_start();
$filename=$fileurl;
header( "Content-type: application/octet-stream ");
header( "Accept-Ranges: bytes ");
header( "Content-Disposition: attachment; filename= '/tmp/test'");
$size=readfile($filename);
header( "Accept-Length: " .$size);
}
$url="http://www.gxlcms.com/article/40485.htm";
downfile($url);
?>

为何页面 http://www.gxlcms.com/article/40485.htm 的内容没有写入 /tmp/test?
而是显示在console中?


回复讨论(解决方案)

你描述的需求与你的代码不相符

你是想吧 网页源码 写进文件?

$url="http://www.gxlcms.com/article/40485.htm";
file_put_contents("/tmp/test", fopen($url, 'r'));

我明白可以这样做,但是为何我的代码不可以?

问题有点多啊。。。
假设你的需求是要把网页html写到一个文件里
1.获取远程链接数据一般用curl实现
2.readfile应该不支持远程吧,不行的话要换file_get_contents或curl
3.ob_start要跟着ob其他函数比如ob_get_contents才能获取到数据
4.只是写入文件要header干什么,又不是导出,header可以全删了
5.最后那个file_put_contents应该不支持直接用句柄吧,先获取数据再放到第二个参数存
6.php中很想有一个配置是配置可不可以获取远程的,check一下

你的代码执行意图有问题,如果需要将web页面写入到文件。

file_put_contents("/tmp/aa.txt",file_get_contents("http://baidu.com"));

人气教程排行