
Apache returns a timeout or crashes when using file_get_contents to receive the contents of a file on a localhost URL.
When you use the php command file_get_contents to receive the contents of a local file, one located on localhost, your script might time out because it reached the maximum execution time. The problem is either a crash of apache because of a never ending for or while loop, or it can have something to do with incorrect DNS settings.
The problem is often that you try to receive the contents of a file in a for or while loop. When you try to get the contents of a file more than x times, apache can crash and has to be restarted (stop and start). Check your script whether this for or while loops are really necessary.
If this is not the problem and you are sure that there is nowhere a hidden for or while loop, it can have something to do with your DNS (domain name system) settings. If you change localhost in your script's URL with a domain name or IP-address such as 127.0.0.1 and it works, you should fix your DNS settings. In Windows you will have to add the line 127.0.0.1 localhost to your C:/Windows/System32/drivers/etc/hosts file. In Unix you will have to change your /etc/hosts file accordingly.
If you use file_get_contents only to check whether a file exists or whether it has a specific size, we suggest to use the file system which is much faster than http requests. You can use simple php commands such as file_exists and filesize to receive information about a file.
Would you like to comment?
Fields with * are obligatory.


