Problem With 'if (fopen())' Stopped Working

I have used the following code for about a year and it has worked perfectly but for some unexplained reason it now fails to function frequently - too frequently to be useful.

The code is designed to check if one or two or both external files exist and if one or the other or both DO exist then to display the contents (images) in my webpage and if the files DO NOT exist then to instead display some local content (image).

What now happens is that only the local content displays even if one or the other or both of the external files do exist.

It is quite important as the code sits on a website page designed for club members that provides information about currently active typhoons in the West Pacific.

I have not changed the code for at least one year so something else must be wrong. I have tried accessing the website page and the function using Microsoft Edge & Firefox on my PC and using the native browser on my mobile - all show that same failure condition.

I have written a piece of simple code to check that the remote files exist and that I can display them in my website page (eliminating the possibility of a remote access restriction) and they files are accessible and will display correctly, but as soon as I reintroduce the ‘if (fopen($url))’ function then I only get the local file displayed.

I have checked that my server has ‘allow_url_fopen = On’

As I said at the beginning, the file has been working fine for a year but for some reason it has basically stopped working.

Perhaps there is a way of rewriting the code and using a different function that will avoid the problem?

I am not an expert in php I have read help threads for the whole day with no result so I am asking for assistance.

The code:

<?php
//error_reporting(0);
$url2 = 'https://www.metoc.navy.mil/jtwc/products/wp0120.gif';
$numb2 = substr($url2,43,2);
$name2 = '';
$url1 = 'https://www.metoc.navy.mil/jtwc/products/wp0220.gif';
$numb1 = substr($url1,43,2);
$name1 = '';
if (fopen($url1, "r") && fopen($url2, "r")) {
echo '<style>#chkstorm{display:none;}</style><h3>' . date('d-M-Y') . ' Image of Typhoon '.$name2.' '.$numb2.'W </h3>
<p>
<a href="'.$url2.'">';
echo '<img src="'.$url2.'" alt="Typhoon '.$name2.' '.$numb2.'W"></a></p>';
echo '<h3>' . date('d-M-Y') . ' Image of Typhoon '.$name1.' '.$numb1.'W</h3>
<p>
<a href="'.$url1.'">';
echo '<img src="'.$url1.'" alt="Typhoon '.$name1.' '.$numb1.'W"></a></p><p class="centxt">The above images are reproduced from Joint Typhoon Warning Center</p><p class="cennormlrgr">How to <a href="prepare-for-a-typhoon.php">prepare for a typhoon</a></p>';
} elseif (fopen($url1, "r"))
{
echo '<style>#chkstorm{display:none;}</style><h3>' . date('d-M-Y') . ' Image of Typhoon '.$name1.' '.$numb1.'W </h3>
<p>
<a href="'.$url1.'">';
echo '<img src="'.$url1.'" alt="Typhoon '.$name1.' '.$numb1.'W"></a></p><p class="centxt">The above image is reproduced from Joint Typhoon Warning Center</p><p class="cennormlrgr">How to <a href="prepare-for-a-typhoon.php">prepare for a typhoon</a></p>';
} elseif (fopen($url2, "r"))
{
echo '<h3>' . date('d-M-Y') . ' Image of Typhoon '.$name2.' '.$numb2.'W </h3>
<p>
<a href="'.$url2.'">';
echo '<img src="'.$url2.'" alt="Typhoon '.$name2.' '.$numb2.'W"></a></p><p class="centxt">The above image is reproduced from Joint Typhoon Warning Center</p><p class="cennormlrgr">How to <a href="prepare-for-a-typhoon.php">prepare for a typhoon</a></p>';

} else {
echo '<style>#chkstorm{display:none;}.nopos{margin:0 auto;width:100%;max-width:680px;display:block;}</style>
<img class="nopos" src="img/nostorms_7265.jpg" alt="typhoon Philippines">';
}
clearstatcache();
?>

If the files definitely exist and your code has not changed, it may be that your host has disabled allow_url_fopen. This ini flag allows fopen to load files over HTTP in the way your code is attempting. I would expect PHP to throw a warning if fopen couldn’t open the file; checking your PHP error logs might help.

Thank you for your reply.

I checked the ini file and allow_url_fopen is enabled.

I have used alternative browsers on alternative machines and the same result is apparent.

The error log shows this:
[14-May-2020 01:47:07 UTC] PHP Warning: fopen(www.metoc.navy.mil/jtwc/products/wp0220.gif): failed to open stream: No such file or directory in /home/beginanew2015/public_html/pgyc.org/chkstorm.php on line 10
[14-May-2020 01:47:07 UTC] PHP Warning: fopen(www.metoc.navy.mil/jtwc/products/wp0220.gif): failed to open stream: No such file or directory in /home/beginanew2015/public_html/pgyc.org/chkstorm.php on line 21
[14-May-2020 01:47:07 UTC] PHP Warning: fopen(www.metoc.navy.mil/jtwc/products/wp0120.gif): failed to open stream: No such file or directory in /home/beginanew2015/public_html/pgyc.org/chkstorm.php on line 29

any ideas welcome . . . :slight_smile:

I should add the that remote files do not always exist on the remote server but at the time I noticed the problem occurring the file ‘www.metoc.navy.mil/jtwc/products/wp0120.gif’ definitely existed and I could independently display it using stripped down code that had no If / Else statements and only intended to retrieve and display.

You need to use either a phpinfo() statement or an ini_get() statement so that you know what value php is actually using at runtime. Also, if error_reporting isn’t E_ALL, you may not be seeing/logging all possible errors.

The fopen() is being executed by your web server. The html <img… markup is being executed by the requesting browser. A browser submits a user agent value with the request. Php statements accessing a url either don’t include a user agent value or state it is php… making the request.

Using CURL to access the value, where you can specify a user agent value, may be required.

If the allow url fopen setting is on, the navy.mil site you are trying to read may have blocked the ip address of your web server (due to too many requests) or may have changed the requirements for accessing the value. They may have documentation on the site telling you how to access the value or they may have created a specific api that you can call (using CURL) to access the value.

thank you for the input and the guidance. I will look into the two statements

I have no direct knowledge of CURL and will have to read up on it.

I have written to the navy.mil site and asked if they can assist but no reply yet . . .

Sponsor our Newsletter | Privacy Policy | Terms of Service