Bläddra i källkod

Parte de Alexander y Kendrick PHP

Kendrick Morales 5 år sedan
förälder
incheckning
70b86ee5ad
1 ändrade filer med 21 tillägg och 20 borttagningar
  1. 21
    20
      reciver.php

+ 21
- 20
reciver.php Visa fil

@@ -1,24 +1,25 @@
1 1
 <?php
2
-	function read_and_delete_first_line($filename)
3
-	{
4
-		$file = file($filename);
5
-		array_shift($file);
6
-		file_put_contents($filename, $file);
7
-	}
8
-	$decoded_location  = json_decode(file_get_contents('php://input'));
9
-	$inTime = gmdate('H:i:s',time());
10
-	$file = 'db.txt';
11
-	$totalLines = intval(exec("wc -l '$file'"));
12
-	//echo $totalLines. "\n"; //For testing purposes
13
-	if($totalLines > 15)
14
-	{
15
-		read_and_delete_first_line("db.txt");
16
-	}
2
+        function read_and_delete_first_line($filename) // This function will delete the first line of db.txt.
3
+        {
4
+                $file = file($filename); //Makes the contents of db.txt into an array.
5
+                array_shift($file); //Removes the first element of the array.
6
+                file_put_contents($filename, $file); //Rewrites db.txt with the contents of the array.
7
+        }
8
+        $decoded_location  = json_decode(file_get_contents('php://input')); //Receives input from app and decodes the json recived.
9
+        $decoded_location->inTime = gmdate('H:i:s',time()); //Adds a new property to the object we get after decoding the json and stores the current time.
10
+	$encoded_location = json_encode($decoded_location); //Reencodes the object back into a json with the new inTime property.
11
+        $file = 'db.txt';
12
+        $totalLines = intval(exec("wc -l '$file'")); //Counts how many lines db.txt has.
13
+        //echo $totalLines. "\n"; //For testing purposes
14
+        if($totalLines > 15) //If db.txt has more than a specified amount of lines, it will delete the first line of db.txt.
15
+        {
16
+                read_and_delete_first_line("db.txt");
17
+        }
17 18
 
18
-	$db = fopen("db.txt","a") or die("FAILED TO ACCESS DATABASE");
19
-	fwrite($db,$decoded_location->latitude. "\t". $decoded_location->longitude. "\t". $inTime. "\n");
20
-	fclose($db);
21
-	//$totalLines = intval(exec("wc -l '$file'")); //For testing purposes
22
-	//echo $totalLines; //For testing purposes
19
+        $db = fopen("db.txt","a") or die("FAILED TO ACCESS DATABASE"); //Opens db.txt in append mode. If it somehow fails to open the database, an error message will be displayed.
20
+        fwrite($db,$encoded_location. "\n"); //Writes the newly encoded json into db.txt.
21
+        fclose($db); //Closes db.txt
22
+        //$totalLines = intval(exec("wc -l '$file'")); //For testing purposes
23
+        //echo $totalLines; //For testing purposes
23 24
 ?>
24 25