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();
?>