What is the fastest way to read a file in PHP? Specifically I’m reading a url, I’m reading a file using fgets, the size of the url is about 1MB and reading 5 url’s will took me 20 seconds at most. I am only getting a line of string, which located at the end portion of the file. I’ve actually use fseek to move the pointer to the end of the url but it only works on files(not url). Any brilliant ideas?
heres my sample code
$fp=fopen("http://url.com", "r");
if(is_bool($fp)){
exit;
}
while(!feof($fp)) {
$data = fgets($fp);
if($data=="this is what i've wanted")
{
// codes...
}
}
fclose($fp);
In this situation you can’t* just skip all of the content. The way network transfers work is that you define a content-length and have to read everything before the part you want.
In short, you can’t just skip it. Just read it in, grab what you need and move on with life.
*Note: Well, you can if the resource supports partial-content and range-requests. Not likely.