开发者

Only partial content from a remote file is being read (PHP)

I need some help with this code. I am pretty sure the code is correct but I could be wrong. The problem is that the getSourceCode() isn't pulling the entire contents of the URL. It only returns a third of the data, for example: the $size variable would return 26301 and the returned data size would only be 8900. I have changed php.ini to have max file size of 100M so I don't think that is problem.

private function getSourceCode($url){     
    $fp = fopen($url, "r");
    $size = str开发者_如何学运维len(file_get_contents($url));;
    $data = fread($fp, $size);
    fclose($fp);
    return $data;
}


Ok, well, if you're using file_get_contents you shouldn't be using fread too.

private function getSourceCode($url){     
    return file_get_contents($url);
}


Short answer is that byte != 1 character in a string. You can use $data= file_get_contents($url) to get the entire file as a string.

Long answer fread is looking for the number of bytes but strlen is returning the number of characters and a character can be larger than 1 byte so you can end up not getting the entire file. Alternatively you could use filesize() to get the file length in bytes instead of strlen().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜