PHP fopen function timed out?

给你一囗甜甜゛ 提交于 2019-12-25 16:48:50

问题


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)


回答1:


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.




回答2:


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.




回答3:


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




回答4:


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.




回答5:


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




回答6:


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.



来源:https://stackoverflow.com/questions/881816/php-fopen-function-timed-out

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!