raj
raj

Reputation:

PHP fopen function timed out?

any idea why fopen would timeout for a file if it is on my server and I know the url is correct?

update: sorry, i should have mentioned this is in php. the code is:

fopen($url, 'r');

It works if i put in a relative path for the file, but not if $url is a url in my server (but it works for google.com). Thanks for the help.

Alaitnik's answer was right. The problem only appears when i access my own server files through the ethernet interface. How can I fix this? I need to be able to access the file from the ethernet interface because the url loads dynamically (it's generated from a wordpress cms, so the url doesn't technically exist as a file on my server)

Upvotes: 0

Views: 8541

Answers (6)

Jaap
Jaap

Reputation: 1

Took me ages to solve this, but here I found it, thanks to Alnitak. Opening the file with localhost in the URL instead of the hostname was what did the trick for me.

Upvotes: 0

arahaya
arahaya

Reputation: 1020

maybe your "allow_url_fopen" is set to "Off" check your php.ini file or phpinfo()

Upvotes: 1

Alnitak
Alnitak

Reputation: 339786

It appears that you're trying to download a file from your own server using the HTTP protocol from a program running on that same server?

If so, the timeout problem is likely to be web server or network configuration related. Timeouts normally only happen because either:

  1. the server really is taking a long time to send back the answer, or
  2. the TCP connection is being blocked

For example, it may be that your local firewall rules only permit access to www.example.com if those queries come from the ethernet interface, but a locally made connection would try to go via the loopback interface.

Upvotes: 3

rojoca
rojoca

Reputation: 11190

Check the comments on the documentation of fopen. There's a whole lot of gold in there.

Upvotes: 0

jrharshath
jrharshath

Reputation: 26583

If you are trying to get the HTML of a URL, I suggest using curl instead of fopen.

fopen is best used with local files, coz it does not "know" how to deal with the idiosyncrasies of a network resource.

Upvotes: 0

x4tje
x4tje

Reputation: 1643

you can use ini_set('default_socket_timeout',2); before opening the fopen $url . This actually set the default socket connection timout without responding. Stream_set_timeout sets time out on the stream that is established via fopn or socket opening functions. Try this may be helpful for you.

Upvotes: 6

Related Questions