View Full Version : Stop A PHP Loop

06-02-2007, 03:59 AM

I have made a dj panel to put on peoples website and it uses the fopen to check the panels version against the lastest one in a text file on my website. The problem is when my site is down the fopen just keeps looping and pretty much destroys the homepage where the "you need to upgrade" message would appear because it can't reach my servers.... how would I stop the PHP loop and display an error if the server/file can't be found?

Heres my syntax:

$filename = "http://www.quickscriptz.ca/radiodjpanel_update.txt";
$handle = fopen("$filename", "r");
$contents = '';
while (!feof($handle)) {
$contents .= fread($handle, 8192);
$_divide = explode("&", $contents);
$_array = 0;
echo "<center>";
while($_divide[$_array] != '')
$result = mysql_query("SELECT panel_version FROM rp_data") or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
$currentversion = $row['panel_version'];
list($_version, $_html, $_message) = explode("~", $_divide[$_array]);
if($_version != $currentversion|$_SESSION['rank'] == "Administrator"){
echo "<blockquote class='exclamation'>A newer version of the Radio DJ Panel is available for download. Click <a href='admin/update.php'>here</a> to update your panel.</blockquote>";
echo "</center>";

Thanks in advance! +Rep for solutions! :thumbsup:

06-02-2007, 04:13 AM
fopen() returns a FALSE if it fails. You need to test for this and take appropriate action -

if($handle = fopen("$filename", "r"))
// code if fopen worked
} else {
// code if fopen failed
}Actually, to write bullet-proof, user friendly code (tell them if something failed instead of just displaying a blank or broken page) every function call needs to use logic like this to do something if it worked and something if it did not.