fread 不能读取完整有以下几种情况1.读取了 length 个字节
2.到达了文件末尾(EOF)
3.a packet becomes available or the socket timeout occurs (for network streams)
4.if the stream is read buffered and it does not represent a plain file, at most one read of up to a number of bytes equal to the chunk size (usually 8192) is made; depending on the previously buffered data, the size of the returned data may be larger than the chunk size.
试试改成这样。<?php
$staTitle = '';
$endTitle = '';
$webAddrs = 'http://www.baidu.com';
@$webFp = fopen($webAddrs,'rb');
if(!$webFp){
echo '打开文件出错';
}
$strWeb = '';
while(!feof($webFp)){
$strWeb .= fread($webFp, 4096);
}
file_put_contents($_SERVER['DOCUMENT_ROOT'] . '/php.txt',$strWeb);
?>
2.到达了文件末尾(EOF)
3.a packet becomes available or the socket timeout occurs (for network streams)
4.if the stream is read buffered and it does not represent a plain file, at most one read of up to a number of bytes equal to the chunk size (usually 8192) is made; depending on the previously buffered data, the size of the returned data may be larger than the chunk size.
试试改成这样。<?php
$staTitle = '';
$endTitle = '';
$webAddrs = 'http://www.baidu.com';
@$webFp = fopen($webAddrs,'rb');
if(!$webFp){
echo '打开文件出错';
}
$strWeb = '';
while(!feof($webFp)){
$strWeb .= fread($webFp, 4096);
}
file_put_contents($_SERVER['DOCUMENT_ROOT'] . '/php.txt',$strWeb);
?>
解决方案 »
- Login 的问题
- 跪求:一个用php实现页面跳转的代码
- php snoopy采集遇到的问题 :bad request your browser sent a request that this server could not understand
- 本地测试没问题,传到服务器上就报错:Warning: fopen(): php_hostconnect: connect failed
- PHP 中如何用 GET/POST 方式分别传递与接收一个数组?
- 刚做完注册页面,帮忙测试:)
- 这么长的字符串如何分割(是长了些),方法不限,只要操作成功,100分立即相送
- 麻烦帮忙检查一下该语句有何错误
- session_start()的问题,请教高手。
- 元旦快乐,新年快乐。散分
- 从api中抓取数据的问题
- 这个地址是怎么来的
2、$strWeb = fread($webFp,strlen(file_get_contents($webAddrs)));
中 file_get_contents($webAddrs) 已经取得了数据,何必再 fread
恩 我是用file_get_contents()来获取打开文件的长度。
但是大大 我还有两个不懂,如果是JS运行了那么我用file_get_contents来获取文件他同样会运行js也就是获取 到的长度是html里面的 和js里面的一共的长度。为什么输出出来了却少了?
这个可以完全读取,为什么fread获取到的只有一部分呢?
$file = $_SERVER['DOCUMENT_ROOT'] . '/php.txt';
file_put_contents($file,file_get_contents("http://www.baidu.com"));