3

I want to open a file on a remote server (e.g. http://example.org/script.php?param=000001). PHP provides several of those methods and I was wondering which method I should take and why? Are there any advantages/disadvantages?

So far I used fopen(), but after several reading-operations it happens that the server did not answer my request and fopen() failed. Afterwards I tried file_get_contents(), which also failed.

I thought it might be the server that blocked my IP, since I opened to many streams. Could this also happen with file_get_contents()?

Which method should I use, When I want to open many files on a server?

What is the technical difference between fopen() and file_get_contents()? And why should the server block my IP? (e.g. servers have limited number of open file streams,... or fopen needs more ressources)

R_User
  • 10,682
  • 25
  • 79
  • 120
  • By "many" do you mean "many simultaneous" or "many, one after another"? – Unsigned Apr 12 '13 at 22:49
  • You can't "open a file" on a server using HTTP. You can make an HTTP request and the server will (may) provide a response. – Rein Henrichs Apr 12 '13 at 22:50
  • Local file: all of the method you've mentioned are fine. Remote file: [cURL](http://ca2.php.net/manual/en/curl.examples-basic.php). – Sammitch Apr 12 '13 at 22:53

1 Answers1

3

file_get_contents() is the way to go

If you have special HTTP header requirements, use the context param

Update:

I see that you have edited the question a little bit. Note, that the difference between fopen and file_get_contents is only that the latter will open the file, read contents, close file, return the contents where the first just opens the file. The process of opening is the same.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • 1
    +1. If you wanna use file_get_contents() with https, check this out: http://stackoverflow.com/questions/1975461/file-get-contents-with-https – Joel Murphy Apr 12 '13 at 22:54