Webmaster Blog

Webmaster Resources

Retrieve a page and save it in a variable with fopen.

Today I have found this little snippet to retrieve a page to save it in a variable e.g. $content.

$handle = fopen($location, "r");
if($handle) { $contents = '';
while (!feof($handle)) { $contents .= fread($handle, 8192); }
fclose($handle);

You want to know what the 8192 does? It’s the byte data the browser retrieves before saving it into $contents.