Search code examples
phpget

PHP: Get parameters from file


I am new to PHP so I could need a bit of help. I want to open a text file and read out some variables. But it doesn't put out a value. It is able to read the file. I think the problem is on the getParameter function.

<?php
    function getParameter($par, $default = null){
        if (isset($_GET[$par]) && strlen($_GET[$par]))
            return $_GET[$par];
        elseif (isset($_POST[$par]) && strlen($_POST[$par]))
            return $_POST[$par];
        else
            return $default;
    }    
    $file = '/var/www/html/gps.txt';
    $lat = getParameter("latitude");
    $lon = getParameter("longitude");
    $time = getParameter("time");
    $sat = getParameter("satellites");
    $speed = getParameter("speed");
    $course = getParameter("course");
    $person = $lat.",".$lon.",".$time.",".$sat.",".$speed.",".$course."\n";

    echo "
    DATA:\n
    Latitude: ".$lat."\n
    Longitude: ".$lon."\n
    Time: ".$time."\n
    Satellites: ".$sat."\n
    Speed: ".$speed."\n
    Course: ".$course;
?>

Expected output: Latitude: 49.xxxxx

Actual output: Latitude:


Solution

  • Your script is wrong. See this change:

    function getMarkers(){
        $file = '/var/www/html/gps.txt'; 
        $file_lines = file($file);
        $array_lines = [];
        foreach ($file_lines as $line) {
            $array_lines[] = explode(',', $line);
        }
        return $array_lines;
    }
    
    $array_lines = getMarkers();
    
    foreach($array_lines as $array_line){
        print_r($array_line);
    }