jhsachs Posted November 28, 2011 Share Posted November 28, 2011 I recently migrated from Windows XP to Windows 7, and a PHP package I depend on (mPDF, see http://www.mpdf1.com/mpdf) stopped working. When I call one of its necessary functions, it hangs. I investigated further and found that the crash was happening in a call to file_get_contents, given one parameter, a URL that points to a CSS file. I found that the file_get_contents and file functions both crash when given a URL. Both work normally when given a pathname on the local file system. As I understand it, these functions are supposed to accept a URL if the INI directive allow_url_fopen is enabled. I checked by calling ini_get; it is. In any case, the php.net description of fopen says that if that directive is disabled, "PHP will emit a warning and the fopen call will fail" -- not just hang! It appears as if there is a glaring bug in one of PHP's core functions. I'm very skeptical that that is really the problem, but I don't know what else to suspect. Any ideas? My configuration is Windows 7 Professional with SP1; Apache 2.2.17; PHP 5.2.17. For reference, here is the script I used to investigate this problem. I stepped through it with a debugger, so I didn't bother displaying results. As shown, the script works. When I comment out the second assignment to $path it should produce identical results; instead it hangs. <?php $isopen = ini_get('allow_url_fopen'); $path = "http://localhost/pdf/cert.css"; $path = "D:/htdocs/pdf/cert.css"; $contents = file($path); $contents = @file($path); $contents = file_get_contents($path); $contents = @file_get_contents($path); ?> Quote Link to comment https://forums.phpfreaks.com/topic/251922-file-functions-hang-when-asked-to-read-url/ Share on other sites More sharing options...
teynon Posted November 28, 2011 Share Posted November 28, 2011 If you go to http://localhost/pdf/cert.css, does the file load or hang? Quote Link to comment https://forums.phpfreaks.com/topic/251922-file-functions-hang-when-asked-to-read-url/#findComment-1291721 Share on other sites More sharing options...
jhsachs Posted November 28, 2011 Author Share Posted November 28, 2011 I'm not sure what you mean by "go to." To me, in this context, "go to" means "load" (into the browser). When I load that URL into the browser it displays the contents of the file. If I try to read the file from that URL in a script, it hangs, as I said. If I load an HTML page that refers to that location, it works normally. (The script actually loads an equivalent relative URL... if that's what you meant, and the difference between a relative and an absolute URL is significant, let me know, and I'll try it.) Quote Link to comment https://forums.phpfreaks.com/topic/251922-file-functions-hang-when-asked-to-read-url/#findComment-1291734 Share on other sites More sharing options...
teynon Posted November 28, 2011 Share Posted November 28, 2011 Ok, if you run his code, does it hang? When you say hang, what does that mean? It times out? It either is going to eventually time out, error out, or get the data (success). <?php echo $file_get_contents("http://www.google.com/"); Quote Link to comment https://forums.phpfreaks.com/topic/251922-file-functions-hang-when-asked-to-read-url/#findComment-1291736 Share on other sites More sharing options...
jhsachs Posted November 28, 2011 Author Share Posted November 28, 2011 Yes, when I run mPDF it hangs. (If it didn't I wouldn't be here, because I would not have a problem! :-) ) "Hang" means that the function never returns control to the script. Eventually the browser times out and I get an apparently complete page load with an empty display. There is no error message from PHP and no timeout from the server. If I'm running the debugger when I do this, I can't start the debugger again unless I restart both the IDE and the browser. That suggests that some part of each component has been left in an undefined state, but that doesn't suggest a possible cause to me. I tried reading a remote page as you suggested, and it works. I tried reading a different local page and it hangs, so the problem appears to be associated with local URLs. That's progress, but I it doesn't suggest a possible cause either. Quote Link to comment https://forums.phpfreaks.com/topic/251922-file-functions-hang-when-asked-to-read-url/#findComment-1291843 Share on other sites More sharing options...
teynon Posted November 28, 2011 Share Posted November 28, 2011 Just on an off chance, try changing localhost to 127.0.0.1 Quote Link to comment https://forums.phpfreaks.com/topic/251922-file-functions-hang-when-asked-to-read-url/#findComment-1291857 Share on other sites More sharing options...
jhsachs Posted November 28, 2011 Author Share Posted November 28, 2011 That works. Very interesting... but not quite a solution, since mPDF derives the URL by adding http://localhost/subdirectory/ to a relative URL taken from a <style> tag. For the grand prize, can you guess why localhost makes it hang when 127.0.0.1 does not? Quote Link to comment https://forums.phpfreaks.com/topic/251922-file-functions-hang-when-asked-to-read-url/#findComment-1291868 Share on other sites More sharing options...
teynon Posted November 28, 2011 Share Posted November 28, 2011 I'm not at a computer rightt now, but google "windows 7 php localhost not working". There is a settinng in windows or apache you have to change to give 127.0.0.1 the alias of localhost Quote Link to comment https://forums.phpfreaks.com/topic/251922-file-functions-hang-when-asked-to-read-url/#findComment-1291881 Share on other sites More sharing options...
jhsachs Posted November 28, 2011 Author Share Posted November 28, 2011 I'm not at my own computer now either, but... be aware that I have no trouble with localhost in the browser's address box, or in other PHP functions that expect URLs. Is this a likely solution despite that? Quote Link to comment https://forums.phpfreaks.com/topic/251922-file-functions-hang-when-asked-to-read-url/#findComment-1291956 Share on other sites More sharing options...
jhsachs Posted November 29, 2011 Author Share Posted November 29, 2011 I checked it out. The file is 'hosts'. I made the change, and it works. Strange. For WinXP they said not to put localhost in hosts, or at least went out of their way to say it didn't matter. Now it does. Thanks for helping me solve this. The connection between symptom and problem was so tenuous that I might never have found it on my own. Quote Link to comment https://forums.phpfreaks.com/topic/251922-file-functions-hang-when-asked-to-read-url/#findComment-1292185 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.