2

I am read a book to build a web service and encountered the "protocol and wrapper" concept in PHP. Jumping into PHP.net document I first saw the file wrapper and really get stuck. Also, the document has no example of it.

Could you give me some real example help me understand file wrapper and it would be great if you could explain it by giving some example because I am totally new with this concept.

2 Answers 2

4

I think that documentation for the file:// wrapper is pretty clear. This is practically the same as when referencing a file by leaving file:// off. For example:

file('file:///path/to/file.txt');
file('/path/to/file.txt');

The above two are equivalent.

Sign up to request clarification or add additional context in comments.

Comments

1

The file:// wrapper is weird and pointless.* You can safely forget that it ever existed.

A much more obviously useful wrapper is the http:// wrapper, which allows you to open and read from remote HTTP resources as if they were files:

$fh = fopen("http://www.google.com/", "r");

Even in this case, it is often better to use a purpose-built library (like cURL) for accessing HTTP resources, as it is better able to represent some of the quirks of HTTP. That being said, the http:// wrapper can be handy for quick prototyping.

*: It's entirely redundant to just opening a file directly, with no wrapper involved. There is no reason I can imagine that you'd ever use it.

1 Comment

Erhm... when you fopen('dir/file.php', 'r') without specifying a protocol, file:// is what PHP defaults to... rather crucial.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.