Advertisement
Guest User

Untitled

a guest
May 23rd, 2014
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.24 KB | None | 0 0
  1. if ( 0 == filesize($filepath.$filename)) {
  2.             $error_message = "file is empty!";
  3.         } else {
  4.             //reading first entry
  5.             $handle = fopen($filepath.$filename, "r");
  6.             if ($handle) {             
  7.                 $obj_array = array();
  8.                 $file_contents = "";
  9.                 do {
  10.                     // process the line read.
  11.                     do {                   
  12.                         $line = trim(fgets($handle));
  13.                         if ($line == false) {
  14.                             echo "line equals false - file has ended!";
  15.                             $fileHasEnded = true;
  16.                             break;
  17.                         }
  18.                     } while ($line == "");
  19.                     if (isset($fileHasEnded) && $fileHasEnded) {
  20.                         $error_message = "file has ended!";
  21.                         break;
  22.                     }
  23.                     if ($line=="Season") {
  24.                         $line = trim(fgets($handle));
  25.                         if ($line == false)
  26.                             break;
  27.                         if (preg_match("/^[0-9a-z]{3,4}$/i", $line)) {
  28.                             if ($line=="SRoc" || $line=="S1E" || preg_match("/^S0[0-9]{1}$/", $line)) {
  29.                                 $Season = $line;
  30.                                 //$season = $line;
  31.                                 $line = trim(fgets($handle));
  32.                                 if ($line == false)
  33.                                     break;
  34.                                 if ($line=="RaceType") {                                   
  35.                                     $line = trim(fgets($handle));
  36.                                     if ($line == false)
  37.                                         break;
  38.                                     if (preg_match("/^[a-z0-9 ]{5,30}$/i", $line)) {
  39.                                         $RaceType = $line;
  40.                                         //$raceType = $line;
  41.                                         $line = trim(fgets($handle));
  42.                                         if ($line == false)
  43.                                             break;
  44.                                         if ($line =="Account") {
  45.                                             $line = trim(fgets($handle));
  46.                                             if ($line == false)
  47.                                                 break;
  48.                                             if (preg_match("/^[a-z0-9_]{3,30}$/i", $line)) {
  49.                                                 $Account = $line;
  50.                                                 $line = trim(fgets($handle));
  51.                                                 if ($line == false)
  52.                                                     break;
  53.                                                 if ($line=="Points") {
  54.                                                     $line = trim(fgets($handle));
  55.                                                     if ($line == false)
  56.                                                         break;
  57.                                                     if (preg_match("/^[0-9]{1,10}$/", $line)) {
  58.                                                         $Points = $line;
  59.                                                         //$obj_array[] = $obj;
  60.                                                         $obj_array[] = (object) array('Season' => $Season, 'RaceType' => $RaceType, 'Account'=>$Account,'Points' => $Points);
  61.                                                         echo "added new entry!";
  62.                                                     }  else {                                  
  63.                                                         $error_message = "Points value '" . $line . "' is not allowed! You can only use digits. From 1 to 10 digits long.";
  64.                                                     }
  65.                                                 } else {
  66.                                                     $fileReadStatus = false;
  67.                                                     $error_message = "Unknown type value found! Expected: Points, Found: " . $line;
  68.                                                     break;
  69.                                                 }
  70.                                             } else {
  71.                                                 $error_message = "Account value '" . $line . "' is not allowed! You can only use '_', english alphabet and digits. From 3 to 30 symbols long.";
  72.                                             }
  73.                                         } else {
  74.                                             $fileReadStatus = false;
  75.                                             $error_message = "Unknown type value found! Expected: Account, Found: " . $line;
  76.                                             break;
  77.                                         }
  78.                                     } else {
  79.                                         $error_message = "Race type value '" . $line . "' is not allowed! You can only use whitespace, english alphabet and digits. From 5 to 30 symbols long.";
  80.                                     }
  81.                                 } else {
  82.                                     $fileReadStatus = false;
  83.                                     $error_message = "Unknown type value found! Expected: RaceType, Found: " . $line;
  84.                                     break;
  85.                                 }
  86.                             } else {
  87.                                 $error_message = "Season value is wrong! Unknown value found: " . $line;
  88.                             }
  89.                         } else {
  90.                             $error_message = "Season value is wrong! Unknown value found: " . $line;
  91.                         }
  92.                     } else {
  93.                         $fileReadStatus = false;
  94.                         $error_message = "Unknown type value found! Expected: Season, Found: " . $line;
  95.                         break;
  96.                     }
  97.                 }
  98.                 while (true);
  99.                 if ($fileReadStatus) {
  100.                     $file_contents = "";
  101.                     foreach ($obj_array as $object) {
  102.                         $object_to_string =  $object->Season . "<br/>";
  103.                         $object_to_string = $object_to_string . $object->RaceType . "<br/>";
  104.                         $object_to_string = $object_to_string . $object->Account . "<br/>";
  105.                         $object_to_string = $object_to_string . $object->Points . "<br/>";
  106.                         $file_contents = $file_contents . $object_to_string;
  107.                     }
  108.                 }
  109.  
  110.  
  111.             } else             
  112.                 $error_message = "There was an error while opening file!";         
  113.             fclose($handle);           
  114.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement