问题描述:

I was trying this simple PHP code from a book.

<?php

$handle=fopen('http://www.abc.com/','r');

//echo $handle;

$response=stream_get_contents($handle);

//echo $response;

$meta=stream_get_meta_data($handle);

//print_r($meta['wrapper_data']);

?>

When we call fopen(), it returns file pointer resource(as per PHP manual). When I echoed the value of $handle, it was a simple number. I did not understand what happened in the background. Is it like fopen() downloaded the html and stored in some where , and give us a pointer?

When I echoed $response, it was entire web page as it is. It means $response contains complete HTML of the web page. Now how it got entire HTML from stream_get_contents($handle). but $handle is just a number if we echo it. I can't understand the mechanism of fopen and file pointer resource. can anyone explain me?

网友答案:

The concept is similar with many programming language (especially C).

When a file is being opened, we need a identifier to manipulate (read / write / delete) this file later, which called as file pointer resource.

When a file is opened by a process using the fopen function, a file handle (file pointer resource) is associated with it until either the process terminates or the handle is closed using the fclose function. The file handle is used to identify the file in many function calls.

More: File Handles in Windows

相关阅读:
Top