0) {
if ($_FILES[$file]["error"] == 2) {
$result = array(FALSE, "Error uploading file: The file was too big.", "");
return $result;
} else if (($_FILES[$file]["error"] == 4) || ($_POST["file"] == "")) {
$result = array(FALSE, "Error uploading file: No file was selected.", "");
return $result;
} else {
$result = array(FALSE, "Error uploading file: PHP error code " . $_FILES[$file]["error"] . ".", "");
return $result;
}
} else {
//This is designed to be a portable/reusable function. However, for the specific purposes of this script,
//I added the extra condition below to check for a "dat" file extension.
if ((in_array($_FILES[$file]["type"], $allowedTypes) === FALSE) || (strtolower(substr($_FILES[$file]["name"], -3)) != "dat")) {
$result = array(FALSE, "Error uploading file: The file type was invalid.", "");
return $result;
} else if ($_FILES[$file]["size"] >= $maxSizeBytes) {
$result = array(FALSE, "Error uploading file: The file was too big.", "");
return $result;
} else {
$result = move_uploaded_file($_FILES[$file]["tmp_name"], $destinationDir . $_FILES[$file]["name"]);
if ($result === FALSE) {
$result = array(FALSE, "Error uploading file. The file appears to have uploaded correctly, but it could not be copied/saved to the web server.", "");
return $result;
} else {
$result = array(TRUE, $_FILES[$file]["name"] . " was uploaded successfully.", $destinationDir . $_FILES[$file]["name"]);
return $result;
}
}
}
}
//General purpose function for allowing users to download a file:
function downloadFile($file) {
if (file_exists($file) === TRUE) {
header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=" . basename($file));
header("Content-Transfer-Encoding: binary");
header("Expires: 0");
header("Cache-Control: must-revalidate");
header("Pragma: public");
header("Content-Length: " . filesize($file));
@ob_clean();
flush();
readfile($file);
}
}
//Create the required variables:
$coordinates = array();
$pattern = "";
$replacement = "";
$i = 0;
$output = "";
$outputFile = "";
//The following variables are for the uploadFile function
$allowedTypes = array("application/octet-stream"); //The list of allowed file types
$maxSizeMB = 10; //The maximum allowed file size (in megabytes)
$maxSizeBytes = (($maxSizeMB * 1024) * 1024); //Converts megabytes to bytes for the script
$file = "file"; //The name attribute of the file upload input element on the HTML form so the data can be retrieved from POST
$destinationDir = "uploads/"; //A directory path where the file should be saved on the web server (the original file name will be preserved)
$results = "";
if (isset($_POST["submit"]) === TRUE) { //If the form has been submitted and the file has been received:
try {
$results = uploadFile($allowedTypes, $maxSizeBytes, $file, $destinationDir);
if ($results[0] === TRUE) {
//Fill an array with the contents of the source file (each line of the file is an element in the array):
$coordinates = file($results[2]);
//Trim the unnecessary header info off the beginning of the file (all lines that begin with "!") and start with the first coordinate:
while (substr($coordinates[0], 0, 1) == "!") {
array_shift($coordinates);
}
array_shift($coordinates);
array_shift($coordinates);
//Go through each line of the source file and format the coordinates properly:
$pattern = "/(\.)([0-9]{1})([0-9]{1})([0-9]{1})([0-9]{1})/";
$replacement = "$1$2$3$4";
foreach ($coordinates as $key=>&$value) {
$coordinates[$key] = trim($coordinates[$key]);
$coordinates[$key] = str_replace("GP ", "", $coordinates[$key]);
$coordinates[$key] = str_replace(" !", "", $coordinates[$key]);
$coordinates[$key] = "N0" . $coordinates[$key];
$coordinates[$key] = str_replace(" ", " W", $coordinates[$key]);
$coordinates[$key] = str_replace(" ", ".", $coordinates[$key]);
$coordinates[$key] = str_replace(".W", " W", $coordinates[$key]);
//The line below trims the coordinates to 3 decimal places. Comment the line out if you want to keep 4 decimal places:
//$coordinates[$key] = preg_replace($pattern, $replacement, $coordinates[$key]);
}
unset($value);
//Go through each line of the source file and structure the output properly:
//This prints 2 coordinates on each line (start lat/long & end lat/long) to create a complete line segment on each line
//It will automatically string together all of the points to create a continuous line until it reaches a line break indicated by "LINE !"
$i = 0;
while (($i + 2) < count($coordinates)) {
$i++;
$output .= "\t\t\t " . $coordinates[$i] . " " . $coordinates[$i + 1] . "\r\n";
$i = $i + 2;
while (strpos($coordinates[$i], "LINE") === FALSE) {
$output .= "\t\t\t " . $coordinates[$i - 1] . " " . $coordinates[$i] . "\r\n";
$i++;
}
}
//Write the output to a file and allow the user to download it:
$outputFile = "downloads/" . mt_rand(0, 9) . mt_rand(0, 9) . mt_rand(0, 9) . mt_rand(0, 9) . ".txt";
file_put_contents($outputFile, $output);
downloadFile($outputFile);
} else {
echo $results[1] . "
Return to form...";
//More error handling here (write to log file, send email alert to admin, etc.)
}
} catch (Exception $error) {
//Error handling here (write to log file, send email alert to admin, etc.)
}
} else {
//If no file was received from POST, output the HTML form for uploading a file:
//Example:
//