about summary refs log tree commit diff
path: root/sourcecodes
diff options
context:
space:
mode:
authorClaude Sonnet 52026-08-17 19:26:43 +0000
committerFrederick Muriuki Muriithi2026-08-17 14:53:08 -0500
commit9cae7ea19c7c744a1ed2bd997571ec1b516a0d32 (patch)
treece73195519a319bb0dc72d75df4d77a8ac584141 /sourcecodes
parent4825da8e49c302392fe6018862dfcdd6f91c6192 (diff)
downloadBNW-9cae7ea19c7c744a1ed2bd997571ec1b516a0d32.tar.gz
Guard fopen() results with a false-check before fwrite/fprintf/fclose
On PHP 7.4, calling fwrite()/fprintf()/fclose() with a false handle
(a failed fopen(), e.g. from a bad path, missing directory, or
permissions issue) just emits a warning and no-ops. On PHP 8.0+, these
functions require a resource argument and throw an uncaught TypeError
instead, turning what used to be a silent degradation into a fatal
crash of the whole request.

This patch wraps every fopen()-then-write call site in the codebase
(18 files) with an `if ($handle !== false) { ... }` check, matching
the existing pattern used elsewhere in the codebase. Where the same
$fpvar handle is threaded through shared helper functions
(enter_ban_list/banlist/whitelist, duplicated across
remove_variables_processing.php, remove_variables_processing_default.php,
modify_structure_learning.php and tier_description_processing_gom.php),
the guard was added inside those functions too for consistency, even
though the call sites are currently dead/commented-out in three of the
four files — so the same landmine doesn't reappear if that code is
ever re-enabled.

Verified empirically against PHP 8.3.28: fopen() on an unwritable path
followed by fwrite() on the resulting `false` throws
  TypeError: fwrite(): Argument #1 ($stream) must be of type resource, false given
without the guard; with the guard, the call is skipped instead of
crashing. The normal (successful fopen) path was also re-run through
mat_structure.php's structure_change() and still produces identical
output to before the change.

Reviewed by: Frederick M Muriithi <fredmanglis@gmail.com>
Diffstat (limited to 'sourcecodes')
-rw-r--r--sourcecodes/add_evd.php30
-rw-r--r--sourcecodes/add_inv.php62
-rw-r--r--sourcecodes/bn_file_load_gom.php4
-rw-r--r--sourcecodes/bn_genenet.php6
-rw-r--r--sourcecodes/create_tiers_gom_part1.php12
-rw-r--r--sourcecodes/example.php26
-rw-r--r--sourcecodes/execute_bn_gom.php34
-rw-r--r--sourcecodes/graphviz_structure.php36
-rw-r--r--sourcecodes/mat_structure.php14
-rw-r--r--sourcecodes/modified_network.php30
-rw-r--r--sourcecodes/modify_structure_learning.php22
-rw-r--r--sourcecodes/net_structure.php10
-rw-r--r--sourcecodes/remove_variables.php12
-rw-r--r--sourcecodes/remove_variables_processing.php22
-rw-r--r--sourcecodes/remove_variables_processing_default.php22
-rw-r--r--sourcecodes/test_set_predictions.php4
-rw-r--r--sourcecodes/tier_description_processing_gom.php48
-rw-r--r--sourcecodes/upload_structure_file.php4
18 files changed, 252 insertions, 146 deletions
diff --git a/sourcecodes/add_evd.php b/sourcecodes/add_evd.php
index 008844b8..21406efd 100644
--- a/sourcecodes/add_evd.php
+++ b/sourcecodes/add_evd.php
@@ -115,16 +115,17 @@ if($dt!=1)
 
 
 
- $ft=$dir."$keyval"."var.txt"; 
- $f1=fopen("$ft","w");  
+ $ft=$dir."$keyval"."var.txt";
+ $f1=fopen("$ft","w");
  $ft=$dir."$keyval"."vardata.txt";
- $f2=fopen("$ft","w");  
+ $f2=fopen("$ft","w");
  $ft=$dir."$keyval"."varname.txt";
  $f3=fopen("$ft","w");
 
 
 $pos = strpos($fpvarname, $sym);
 
+if ($f1 !== false && $f2 !== false && $f3 !== false) {
 if($fpvarname=="")
 {
   fwrite($f1,$vriable);
@@ -132,11 +133,11 @@ if($fpvarname=="")
   fwrite($f3,$sym);
 
 }
-else 
+else
 {
-  if ($pos === false)// append at buttom as new records 
+  if ($pos === false)// append at buttom as new records
   {
-  
+
      $fpvar=$fpvar."\t".$vriable;
      fwrite($f1,$fpvar);
 
@@ -147,7 +148,7 @@ else
      fwrite($f3,$fpvarname);
 
   }
-  else 
+  else
   {
      $cld=explode("\t",$fpvarname);
      $j=0;
@@ -158,12 +159,12 @@ else
         if($cc==$sym)
         {
               $inx=$j;
-        } 
+        }
         $j++;
-    
+
      }
 
- 
+
      fwrite($f1,$fpvar);
      fwrite($f3,$fpvarname);
 
@@ -178,18 +179,19 @@ else
               $inx=$j;
               fprintf($f2,"%s\t",$textdata);
         }
-        else 
-        {          
+        else
+        {
               fprintf($f2,"%s\t",$cc);
         }
-        
+
         $j++;
-    
+
      }
 
    }
 
 }
+}
 
 
   //execute shell script for matlab
diff --git a/sourcecodes/add_inv.php b/sourcecodes/add_inv.php
index 26d089ed..61845a46 100644
--- a/sourcecodes/add_inv.php
+++ b/sourcecodes/add_inv.php
@@ -107,16 +107,17 @@ if($dt!=1)
   $textdata=discretemap($textdata,$sym,$dmapdata);
 
 
- $ft=$dir.$keyval."var.txt"; 
- $f1=fopen("$ft","w");  
+ $ft=$dir.$keyval."var.txt";
+ $f1=fopen("$ft","w");
  $ft=$dir.$keyval."vardata.txt";
- $f2=fopen("$ft","w");  
+ $f2=fopen("$ft","w");
  $ft=$dir.$keyval."varname.txt";
  $f3=fopen("$ft","w");
 
 
 $pos = strpos($fpvarname, $sym);
 
+if ($f1 !== false && $f2 !== false && $f3 !== false) {
 if($fpvarname=="")
 {
   fwrite($f1,$vriable);
@@ -124,11 +125,11 @@ if($fpvarname=="")
   fwrite($f3,$sym);
 
 }
-else 
+else
 {
-  if ($pos === false)// append at buttom as new records 
+  if ($pos === false)// append at buttom as new records
   {
-  
+
      $fpvar=$fpvar."\t".$vriable;
      fwrite($f1,$fpvar);
 
@@ -139,7 +140,7 @@ else
      fwrite($f3,$fpvarname);
 
   }
-  else 
+  else
   {
      $cld=explode("\t",$fpvarname);
      $j=0;
@@ -150,12 +151,12 @@ else
         if($cc==$sym)
         {
               $inx=$j;
-        } 
+        }
         $j++;
-    
+
      }
 
- 
+
      fwrite($f1,$fpvar);
      fwrite($f3,$fpvarname);
 
@@ -170,18 +171,19 @@ else
               $inx=$j;
               fprintf($f2,"%s\t",$textdata);
         }
-        else 
-        {          
+        else
+        {
               fprintf($f2,"%s\t",$cc);
         }
-        
+
         $j++;
-    
+
      }
 
    }
 
 }
+}
 
 include("mat_structure.php");
 $keyval = valid_keyval($keyval);
@@ -222,17 +224,21 @@ $n=count($dataname);
 
 $initialstring="digraph G {\n"."size=\"6,8\";  ratio = fill;\n"."node [shape=square,width=1.5];\n";
 $endstring="}";
-fwrite($fout,"$initialstring");
+if ($fout !== false) {
+  fwrite($fout,"$initialstring");
+}
 
 
 //$g_file_name="./data/".$keyval."grviz_name_file_new.txt";
 $g_file_name=$dir.$keyval."grviz_name_file_new.txt";
 $grviz_name_file=fopen($g_file_name,"w");
 
-for($i=0;$i<$n;$i++)
-{
-      $row_name=trim($dataname[$i]);  
-      fwrite($grviz_name_file,"$row_name\n");
+if ($grviz_name_file !== false) {
+  for($i=0;$i<$n;$i++)
+  {
+        $row_name=trim($dataname[$i]);
+        fwrite($grviz_name_file,"$row_name\n");
+  }
 }
 
 
@@ -249,17 +255,21 @@ for($i=1;$i<=$n;$i++)
 	   {
                $col_val=trim($col_arr[$j]);
                if($col_val==1)
-               { 
+               {
                   $col_name=trim($dataname[$j]);
-                  fprintf($fout,"%s -> %s;\n",$ii,$j);  
+                  if ($fout !== false) {
+                    fprintf($fout,"%s -> %s;\n",$ii,$j);
+                  }
+
 
-                 
 	        }
           }
-          
-     
-} 
-fwrite($fout,"$endstring");   
+
+
+}
+if ($fout !== false) {
+  fwrite($fout,"$endstring");
+}
 
 ?>
 <script>
diff --git a/sourcecodes/bn_file_load_gom.php b/sourcecodes/bn_file_load_gom.php
index 4f1d1a3b..0074d673 100644
--- a/sourcecodes/bn_file_load_gom.php
+++ b/sourcecodes/bn_file_load_gom.php
@@ -141,7 +141,9 @@ if($searchID!="")
   if ($UploadValue=="NO")
   {
       $fpdata = fopen($dir.$keyval."continuous_input_orig.txt","w");
-      fwrite($fpdata,$searchID);
+      if ($fpdata !== false) {
+        fwrite($fpdata,$searchID);
+      }
   }  
   $keyval = valid_keyval($keyval);
   shell_exec('./run_scripts/run_prep_input '.$keyval);
diff --git a/sourcecodes/bn_genenet.php b/sourcecodes/bn_genenet.php
index 8fd5aea7..d443cee4 100644
--- a/sourcecodes/bn_genenet.php
+++ b/sourcecodes/bn_genenet.php
@@ -161,8 +161,10 @@ if($searchID!="")
   if ($UploadValue=="NO")
   {
       $fpdata = fopen($dir.$keyval."continuous_input_orig.txt","w");
-      fwrite($fpdata,$searchID);
-      fclose($fpdata);
+      if ($fpdata !== false) {
+        fwrite($fpdata,$searchID);
+        fclose($fpdata);
+      }
   }  
   $keyval = valid_keyval($keyval);
   shell_exec('./run_scripts/run_prep_input '.$keyval);
diff --git a/sourcecodes/create_tiers_gom_part1.php b/sourcecodes/create_tiers_gom_part1.php
index 891d0170..6b90c015 100644
--- a/sourcecodes/create_tiers_gom_part1.php
+++ b/sourcecodes/create_tiers_gom_part1.php
@@ -69,17 +69,23 @@ $maxplist=$node-1;
 //print default number of parents
 $pfile=$dir.$keyval."parent.txt";
 $parentf=fopen($pfile,"w");
-fwrite($parentf,"$parent_number\n");
+if ($parentf !== false) {
+  fwrite($parentf,"$parent_number\n");
+}
 
 //print default number of k for model averaging
 $kfile=$dir.$keyval."k.txt";
 $kf=fopen($kfile,"w");
-fwrite($kf,"$k_number\n");
+if ($kf !== false) {
+  fwrite($kf,"$k_number\n");
+}
 
 //print model averaging threshold
 $thrfile=$dir.$keyval."thr.txt";
 $kf=fopen($thrfile,"w");
-fwrite($kf,"$structure_thr\n");
+if ($kf !== false) {
+  fwrite($kf,"$structure_thr\n");
+}
 
 //////////////////Check execution time//////////////////////////////////////////////
 $keyval=valid_keyval($keyval);
diff --git a/sourcecodes/example.php b/sourcecodes/example.php
index 47570aa2..d132bae8 100644
--- a/sourcecodes/example.php
+++ b/sourcecodes/example.php
@@ -47,16 +47,20 @@ $n=count($dataname);
 
 $initialstring="digraph G {\n"."size=\"6,8\";\n"."node [shape=square,width=1.5];\n";
 $endstring="}";
-fwrite($fout,"$initialstring");
+if ($fout !== false) {
+  fwrite($fout,"$initialstring");
+}
 
 $g_file_name=$dir.$keyval."grviz_name_file.txt";
 $grviz_name_file=fopen($g_file_name,"w");
 
-for($i=0;$i<$n;$i++)
-  {
-    $row_name=trim($dataname[$i]);
-    fwrite($grviz_name_file,"$row_name\n");
-  }
+if ($grviz_name_file !== false) {
+  for($i=0;$i<$n;$i++)
+    {
+      $row_name=trim($dataname[$i]);
+      fwrite($grviz_name_file,"$row_name\n");
+    }
+}
 
 for($i=1;$i<=$n;$i++)
   {
@@ -71,7 +75,9 @@ for($i=1;$i<=$n;$i++)
 	if($col_val==1)
 	  {
 	    $col_name=trim($dataname[$j]);
-	    fprintf($fout,"%s -> %s;\n",$ii,$j);
+	    if ($fout !== false) {
+	      fprintf($fout,"%s -> %s;\n",$ii,$j);
+	    }
 	    //fprintf($fouttemp,"%s -> %s;\n",$row_name,$col_name);
 
 	  }
@@ -79,8 +85,10 @@ for($i=1;$i<=$n;$i++)
 
 
   }
-fwrite($fout,"$endstring");
-fclose($fout);
+if ($fout !== false) {
+  fwrite($fout,"$endstring");
+  fclose($fout);
+}
 
 
 shell_exec('./run_scripts/run_octave '.$keyval);
diff --git a/sourcecodes/execute_bn_gom.php b/sourcecodes/execute_bn_gom.php
index 8a728a24..93d46309 100644
--- a/sourcecodes/execute_bn_gom.php
+++ b/sourcecodes/execute_bn_gom.php
@@ -48,15 +48,19 @@ $n=count($dataname);
 
 $initialstring="digraph G {\n"."size=\"6,8\";  ratio = fill;\n"."node [shape=square,width=1.5];\n";
 $endstring="}";
-fwrite($fout,"$initialstring");
+if ($fout !== false) {
+  fwrite($fout,"$initialstring");
+}
 
 $g_file_name=$dir.$keyval."grviz_name_file.txt";
 $grviz_name_file=fopen($g_file_name,"w");
 
-for($i=0;$i<$n;$i++)
-{
-      $row_name=trim($dataname[$i]);  
-      fwrite($grviz_name_file,"$row_name\n");
+if ($grviz_name_file !== false) {
+  for($i=0;$i<$n;$i++)
+  {
+        $row_name=trim($dataname[$i]);
+        fwrite($grviz_name_file,"$row_name\n");
+  }
 }
 
 for($i=1;$i<=$n;$i++)
@@ -71,18 +75,22 @@ for($i=1;$i<=$n;$i++)
 	   {
                $col_val=trim($col_arr[$j]);
                if($col_val==1)
-               { 
+               {
                   $col_name=trim($dataname[$j]);
-                  fprintf($fout,"%s -> %s;\n",$ii,$j);  
+                  if ($fout !== false) {
+                    fprintf($fout,"%s -> %s;\n",$ii,$j);
+                  }
                   //fprintf($fouttemp,"%s -> %s;\n",$row_name,$col_name);
-                 
+
 	        }
           }
-          
-     
-} 
-fwrite($fout,"$endstring");   
-fclose($fout);
+
+
+}
+if ($fout !== false) {
+  fwrite($fout,"$endstring");
+  fclose($fout);
+}
 
 
 shell_exec('./run_scripts/run_octave '.$keyval);
diff --git a/sourcecodes/graphviz_structure.php b/sourcecodes/graphviz_structure.php
index 1e866bc4..641a4854 100644
--- a/sourcecodes/graphviz_structure.php
+++ b/sourcecodes/graphviz_structure.php
@@ -40,7 +40,9 @@ $n=count($dataname);
 
 $initialstring="digraph G {\n"."size=\"6,8\";\n"."node [shape=square,width=1.5];\n";
 $endstring="}";
-fwrite($fout,"$initialstring");
+if ($fout !== false) {
+  fwrite($fout,"$initialstring");
+}
 //fwrite($fouttemp,"$initialstring_temp");
 
 
@@ -48,10 +50,12 @@ fwrite($fout,"$initialstring");
 $g_file_name="/tmp/bnw/".$keyval."grviz_name_file.txt";
 $grviz_name_file=fopen($g_file_name,"w");
 
-for($i=0;$i<$n;$i++)
-{
-      $row_name=trim($dataname[$i]);  
-      fwrite($grviz_name_file,"$row_name\n");
+if ($grviz_name_file !== false) {
+  for($i=0;$i<$n;$i++)
+  {
+        $row_name=trim($dataname[$i]);
+        fwrite($grviz_name_file,"$row_name\n");
+  }
 }
 
 for($i=1;$i<=$n;$i++)
@@ -67,17 +71,21 @@ for($i=1;$i<=$n;$i++)
 	   {
                $col_val=trim($col_arr[$j]);
                if($col_val==1)
-               { 
+               {
                   $col_name=trim($dataname[$j]);
-                  fprintf($fout,"%s -> %s;\n",$ii,$j);  
+                  if ($fout !== false) {
+                    fprintf($fout,"%s -> %s;\n",$ii,$j);
+                  }
                   //fprintf($fouttemp,"%s -> %s;\n",$row_name,$col_name);
-                 
+
 	        }
           }
           
      
-} 
-fwrite($fout,"$endstring");   
+}
+if ($fout !== false) {
+  fwrite($fout,"$endstring");
+}
 //fwrite($fouttemp,"$endstring");
  
 //shell_exec('/usr/bin/dot -Tpng -o /var/www/html/compbio/BNW/graphviz.jpg /var/www/html/compbio/BNW/graphviztemp.txt');
@@ -87,9 +95,11 @@ fwrite($fout,"$endstring");
 $file1="/tmp/bnw/".$keyval."run_initialstructure.sh";
 $initiallines=file_get_contents("/tmp/bnw/temp_shell_file_initial_structure");
 $all_lines="$initiallines"."$keyval\nfi\nexit";
-$fp = fopen($file1,"w"); 
-fwrite($fp, "$all_lines\n");
-fclose($fp);
+$fp = fopen($file1,"w");
+if ($fp !== false) {
+  fwrite($fp, "$all_lines\n");
+  fclose($fp);
+}
 //prepare and execute shell script for matlab with a write lock
 //$cmd="./runmat.sh $keyval";
 //system($cmd);
diff --git a/sourcecodes/mat_structure.php b/sourcecodes/mat_structure.php
index 29bdb854..f4e80290 100644
--- a/sourcecodes/mat_structure.php
+++ b/sourcecodes/mat_structure.php
@@ -48,6 +48,7 @@ $i++;
 
 
 $j=0;
+if ($fp !== false) {
 for($l=0;$l<=$i;$l++)
 {
   $line=$arrname[$l];
@@ -55,23 +56,24 @@ for($l=0;$l<=$i;$l++)
   if($j==0)
   {
     fwrite($fp,$line);
-    
+
   }
   else
-  {  
-   
+  {
+
     $data_cell=explode("\t",$line);
-  
+
     for($k=0;$k<$i;$k++)
     {
        $cell=$data_cell[$k]*$cell_val[$k];
        fprintf($fp,"%s\t",$cell);
-    } 
+    }
   }
   fprintf($fp,"\n");
- 
+
   $j++;
 }
+}
 
 //return $matfilestruct;
 }
diff --git a/sourcecodes/modified_network.php b/sourcecodes/modified_network.php
index 9f38335c..3be0d69c 100644
--- a/sourcecodes/modified_network.php
+++ b/sourcecodes/modified_network.php
@@ -31,20 +31,22 @@ if($_POST["My_key"]!="")
 $filename = "/tmp/bnw/".$old_key."modify_edge.txt";
 
 $file = fopen($filename,'w');
-//fwrite($file,$json);
-fwrite($file,$nnodes);
-fwrite($file,"\n");
-fwrite($file,$node_labs);
-fwrite($file,"\n");
-fwrite($file,$nedges);
-fwrite($file,"\n");
-fwrite($file,$sources);
-fwrite($file,"\n");
-fwrite($file,$targets);
-fwrite($file,"\n");
-fwrite($file,$weights);
-fwrite($file,"\n");
-fclose($file);
+if ($file !== false) {
+  //fwrite($file,$json);
+  fwrite($file,$nnodes);
+  fwrite($file,"\n");
+  fwrite($file,$node_labs);
+  fwrite($file,"\n");
+  fwrite($file,$nedges);
+  fwrite($file,"\n");
+  fwrite($file,$sources);
+  fwrite($file,"\n");
+  fwrite($file,$targets);
+  fwrite($file,"\n");
+  fwrite($file,$weights);
+  fwrite($file,"\n");
+  fclose($file);
+}
 
 shell_exec('./run_scripts/run_mod_edges '.$old_key.' '.$keyval);
 
diff --git a/sourcecodes/modify_structure_learning.php b/sourcecodes/modify_structure_learning.php
index 1b0a70ef..c4e66bf0 100644
--- a/sourcecodes/modify_structure_learning.php
+++ b/sourcecodes/modify_structure_learning.php
@@ -11,8 +11,10 @@ $dir="/tmp/bnw/";
 $tf=$dir.$keyval."del_var.txt";
 $fpvar = fopen("$tf","w");
 
-fwrite($fpvar,"$tier");
-fclose($fpvar);
+if ($fpvar !== false) {
+  fwrite($fpvar,"$tier");
+  fclose($fpvar);
+}
 }
 
 
@@ -28,9 +30,11 @@ function enter_ban_list($t1,$s1,$t2,$s2,$tier_d,$fpvar)
             $data_val_2=$tier_d[$t2][$j];
             if($data_val_1!=$data_val_2)
             {
-               
-               fprintf($fpvar,"%s\t%s\n",$data_val_1,$data_val_2); 
-              
+
+               if ($fpvar !== false) {
+                 fprintf($fpvar,"%s\t%s\n",$data_val_1,$data_val_2);
+               }
+
 
             }
    
@@ -151,7 +155,9 @@ for ($i=0;$i<$n;$i+=2)
 
 	if($d1!="" && $d2!="")
 	{
-  		fprintf($fpvar,"%s\t%s\n",$d1,$d2);
+  		if ($fpvar !== false) {
+  		  fprintf($fpvar,"%s\t%s\n",$d1,$d2);
+  		}
   		$nn++;
 	}
 
@@ -177,7 +183,9 @@ $d2=trim($data[$ii]);
 
 if($d1!="" && $d2!="")
 {
-  fprintf($fpvar,"%s\t%s\n",$d1,$d2);
+  if ($fpvar !== false) {
+    fprintf($fpvar,"%s\t%s\n",$d1,$d2);
+  }
   $nn++;
 }
 
diff --git a/sourcecodes/net_structure.php b/sourcecodes/net_structure.php
index dfe14b99..49abbe06 100644
--- a/sourcecodes/net_structure.php
+++ b/sourcecodes/net_structure.php
@@ -109,11 +109,13 @@ if($searchID!="")
 
   $fpmain = fopen($TextinFile,"w");
 
-  foreach($str_arr as $line)
-  {
-    $line=trim($line);
-    fwrite($fpmain, "$line\n");
+  if ($fpmain !== false) {
+    foreach($str_arr as $line)
+    {
+      $line=trim($line);
+      fwrite($fpmain, "$line\n");
 
+    }
   }
       }
 ?>
diff --git a/sourcecodes/remove_variables.php b/sourcecodes/remove_variables.php
index 0fd30136..0d925050 100644
--- a/sourcecodes/remove_variables.php
+++ b/sourcecodes/remove_variables.php
@@ -69,17 +69,23 @@ $maxplist=$node-1;
 //print default number of parents
 $pfile=$dir.$keyval."parent.txt";
 $parentf=fopen($pfile,"w");
-fwrite($parentf,"$parent_number\n");
+if ($parentf !== false) {
+  fwrite($parentf,"$parent_number\n");
+}
 
 //print default number of k for model averaging
 $kfile=$dir.$keyval."k.txt";
 $kf=fopen($kfile,"w");
-fwrite($kf,"$k_number\n");
+if ($kf !== false) {
+  fwrite($kf,"$k_number\n");
+}
 
 //print model averaging threshold
 $thrfile=$dir.$keyval."thr.txt";
 $kf=fopen($thrfile,"w");
-fwrite($kf,"$structure_thr\n");
+if ($kf !== false) {
+  fwrite($kf,"$structure_thr\n");
+}
 
 //////////////////Check execution time//////////////////////////////////////////////
 $keyval=valid_keyval($keyval);
diff --git a/sourcecodes/remove_variables_processing.php b/sourcecodes/remove_variables_processing.php
index 1d3aba2c..a0bd9d22 100644
--- a/sourcecodes/remove_variables_processing.php
+++ b/sourcecodes/remove_variables_processing.php
@@ -11,8 +11,10 @@ $dir="/tmp/bnw/";
 $tf=$dir.$keyval."del_var.txt";
 $fpvar = fopen("$tf","w");
 
-fwrite($fpvar,"$tier");
-fclose($fpvar);
+if ($fpvar !== false) {
+  fwrite($fpvar,"$tier");
+  fclose($fpvar);
+}
 }
 
 
@@ -28,9 +30,11 @@ function enter_ban_list($t1,$s1,$t2,$s2,$tier_d,$fpvar)
             $data_val_2=$tier_d[$t2][$j];
             if($data_val_1!=$data_val_2)
             {
-               
-               fprintf($fpvar,"%s\t%s\n",$data_val_1,$data_val_2); 
-              
+
+               if ($fpvar !== false) {
+                 fprintf($fpvar,"%s\t%s\n",$data_val_1,$data_val_2);
+               }
+
 
             }
    
@@ -152,7 +156,9 @@ for ($i=0;$i<$n;$i+=2)
 
 	if($d1!="" && $d2!="")
 	{
-  		fprintf($fpvar,"%s\t%s\n",$d1,$d2);
+  		if ($fpvar !== false) {
+  		  fprintf($fpvar,"%s\t%s\n",$d1,$d2);
+  		}
   		$nn++;
 	}
 
@@ -178,7 +184,9 @@ $d2=trim($data[$ii]);
 
 if($d1!="" && $d2!="")
 {
-  fprintf($fpvar,"%s\t%s\n",$d1,$d2);
+  if ($fpvar !== false) {
+    fprintf($fpvar,"%s\t%s\n",$d1,$d2);
+  }
   $nn++;
 }
 
diff --git a/sourcecodes/remove_variables_processing_default.php b/sourcecodes/remove_variables_processing_default.php
index 3c5ffb68..102fffea 100644
--- a/sourcecodes/remove_variables_processing_default.php
+++ b/sourcecodes/remove_variables_processing_default.php
@@ -11,8 +11,10 @@ $dir="/tmp/bnw/";
 $tf=$dir.$keyval."del_var.txt";
 $fpvar = fopen("$tf","w");
 
-fwrite($fpvar,"$tier");
-fclose($fpvar);
+if ($fpvar !== false) {
+  fwrite($fpvar,"$tier");
+  fclose($fpvar);
+}
 }
 
 
@@ -28,9 +30,11 @@ function enter_ban_list($t1,$s1,$t2,$s2,$tier_d,$fpvar)
             $data_val_2=$tier_d[$t2][$j];
             if($data_val_1!=$data_val_2)
             {
-               
-               fprintf($fpvar,"%s\t%s\n",$data_val_1,$data_val_2); 
-              
+
+               if ($fpvar !== false) {
+                 fprintf($fpvar,"%s\t%s\n",$data_val_1,$data_val_2);
+               }
+
 
             }
    
@@ -152,7 +156,9 @@ for ($i=0;$i<$n;$i+=2)
 
 	if($d1!="" && $d2!="")
 	{
-  		fprintf($fpvar,"%s\t%s\n",$d1,$d2);
+  		if ($fpvar !== false) {
+  		  fprintf($fpvar,"%s\t%s\n",$d1,$d2);
+  		}
   		$nn++;
 	}
 
@@ -178,7 +184,9 @@ $d2=trim($data[$ii]);
 
 if($d1!="" && $d2!="")
 {
-  fprintf($fpvar,"%s\t%s\n",$d1,$d2);
+  if ($fpvar !== false) {
+    fprintf($fpvar,"%s\t%s\n",$d1,$d2);
+  }
   $nn++;
 }
 
diff --git a/sourcecodes/test_set_predictions.php b/sourcecodes/test_set_predictions.php
index 076188b4..f1806575 100644
--- a/sourcecodes/test_set_predictions.php
+++ b/sourcecodes/test_set_predictions.php
@@ -189,7 +189,9 @@ if($searchID!="")
     {
       //      $fpdata = fopen("./data/".$keyval."ts_input.txt","w");
       $fpdata = fopen($dir.$keyval."ts_upload.txt","w");
-      fwrite($fpdata,$searchID);
+      if ($fpdata !== false) {
+        fwrite($fpdata,$searchID);
+      }
     }
   $command = './run_scripts/run_test_set '.$keyval;
   $output = shell_exec("$command > /dev/null 2 > /dev/null &");
diff --git a/sourcecodes/tier_description_processing_gom.php b/sourcecodes/tier_description_processing_gom.php
index f6e9eee2..55d315cb 100644
--- a/sourcecodes/tier_description_processing_gom.php
+++ b/sourcecodes/tier_description_processing_gom.php
@@ -10,26 +10,34 @@ $tier=trim($_GET['tier']);
 $dir="/tmp/bnw/";
 $tf=$dir.$keyval."tier.txt";
 $fpvar = fopen("$tf","w");
-fwrite($fpvar,"$tier");
-fclose($fpvar);
+if ($fpvar !== false) {
+  fwrite($fpvar,"$tier");
+  fclose($fpvar);
+}
 
 $tierdesc=trim($_GET['tierdesc']);
 $tdf=$dir.$keyval."tierdesc.txt";
 $fpvard = fopen("$tdf","w");
-fwrite($fpvard,"$tierdesc");
-fclose($fpvard);
+if ($fpvard !== false) {
+  fwrite($fpvard,"$tierdesc");
+  fclose($fpvard);
+}
 
 $ban=trim($_GET['ban']);
 $banf=$dir.$keyval."ban.txt";
 $fpvarban = fopen("$banf","w");
-fwrite($fpvarban,"$ban");
-fclose($fpvarban);
+if ($fpvarban !== false) {
+  fwrite($fpvarban,"$ban");
+  fclose($fpvarban);
+}
 
 $white=trim($_GET['white']);
 $whitef=$dir.$keyval."white.txt";
 $fpvarwhite = fopen("$whitef","w");
-fwrite($fpvarwhite,"$white");
-fclose($fpvarwhite);
+if ($fpvarwhite !== false) {
+  fwrite($fpvarwhite,"$white");
+  fclose($fpvarwhite);
+}
 
 
 }
@@ -47,9 +55,11 @@ function enter_ban_list($t1,$s1,$t2,$s2,$tier_d,$fpvar)
             $data_val_2=$tier_d[$t2][$j];
             if($data_val_1!=$data_val_2)
             {
-               
-               fprintf($fpvar,"%s\t%s\n",$data_val_1,$data_val_2); 
-              
+
+               if ($fpvar !== false) {
+                 fprintf($fpvar,"%s\t%s\n",$data_val_1,$data_val_2);
+               }
+
 
             }
    
@@ -170,7 +180,9 @@ for ($i=0;$i<$n;$i+=2)
 
 	if($d1!="" && $d2!="")
 	{
-  		fprintf($fpvar,"%s\t%s\n",$d1,$d2);
+  		if ($fpvar !== false) {
+  		  fprintf($fpvar,"%s\t%s\n",$d1,$d2);
+  		}
   		$nn++;
 	}
 
@@ -195,7 +207,9 @@ $d2=trim($data[$ii]);
 
 if($d1!="" && $d2!="")
 {
-  fprintf($fpvar,"%s\t%s\n",$d1,$d2);
+  if ($fpvar !== false) {
+    fprintf($fpvar,"%s\t%s\n",$d1,$d2);
+  }
   $nn++;
 }
 
@@ -226,8 +240,12 @@ $Textwhite=$sid2.".txt";
 $fpb = fopen($Textban,"w");
 $fpw = fopen($Textwhite,"w");
 $datpost="From\tTo\n";
-fwrite($fpb,"$datpost");
-fwrite($fpw,"$datpost");
+if ($fpb !== false) {
+  fwrite($fpb,"$datpost");
+}
+if ($fpw !== false) {
+  fwrite($fpw,"$datpost");
+}
 
 describe_tier($fpb,$keyval);
 banlist($fpb,$ban);
diff --git a/sourcecodes/upload_structure_file.php b/sourcecodes/upload_structure_file.php
index 0b980b25..2b235dd8 100644
--- a/sourcecodes/upload_structure_file.php
+++ b/sourcecodes/upload_structure_file.php
@@ -122,7 +122,9 @@ if($searchID!="")
   if ($UploadValue=="NO")
   {
       $fpdata = fopen($dir.$keyval."continuous_input_orig.txt","w");
-      fwrite($fpdata,$searchID);
+      if ($fpdata !== false) {
+        fwrite($fpdata,$searchID);
+      }
   }  
   shell_exec('./run_scripts/run_prep_input '.$keyval);
 ?>