Wednesday, July 10, 2013

iMapIt Pro 2.1 – How the Transferring of a File Exported to a Web Site Works Expanded


In the previous article we described the new iMapIt Pro application feature to transfer files through the Web, introduced in version 2.0.
In version 2.1 this new feature was expanded to send one additional information through the Web along with the exported file: it was included the Post parameter "DeviceId".
The Device ID is a unique identification code of your iPhone or iPad, which can be handled on the server that receives the file to define authorizations, accesses, or simply to identify the origin of the received file.

What changes in the reception of the file on the server?

The PHP script titled "imapit_uploader.php" presented in the previous article as an example for receiving the exported file, now can receive and handle this new parameter. In the example below the DeviceId is only displayed on the response screen. See the source of the current PHP script below.

------------------------------------------------------------- Beginning of PHP file (do not include this line)


<?php

// iMapIt Uploader
// Example of PHP script to upload the export file sent by iMapIt

// Folder where the file will be saved
$_UP['pasta'] = 'uploads/';

// File maximum size (in Bytes)
$_UP['tamanho'] = 1024 * 1024 * 2// 2Mb

// Array with allowed file extensions
$_UP['extensoes'] = array('dxf''kml''gpx');

// Rename file? (If true, file will be saved with a unique name.jpg)
$_UP['renomeia'] = false;

// Array with PHP upload common error description
$_UP['erros'][0] = 'Successful upload';
$_UP['erros'][1] = 'File size is greater than PHP limit';
$_UP['erros'][2] = 'File size is greater than the limit specified in HTML';
$_UP['erros'][3] = 'Partial file upload';
$_UP['erros'][4] = 'File not uploaded';

//  Shows the header
echo "<br><br><br>--- iMapIt Uploader ---<br><br>";

// Shows the device ID received inside POST variable DeviceID
echo "Device ID:" . $_POST['DeviceId'. "<br><br>";
    
// Shows the file name
echo "File name: " . $_FILES['iMapIt_File']['name'. "<br><br>";

// Check if there were any upload error. If yes, show error message.
if ($_FILES['iMapIt_File']['error'] != 0) {
die("It was not possible to upload the file, error:<br />" . $_UP['erros'][$_FILES['iMapIt_File']['error']]);
exit// Stop script execution
}

// If the script got to this point there was no upload error and the PHP script can continue

// Check if it is a allowed file extension
$extensao = strtolower(end(explode('.', $_FILES['iMapIt_File']['name'])));
if (array_search($extensao, $_UP['extensoes']) === false) {
    echo "Please, the only allowed file extensions are: dxf, kml ou gpx";
}

// Check file size
else if ($_UP['tamanho'] < $_FILES['iMapIt_File']['size']) {
echo "Uploaded file is to big. Send only files up to 2Mb.";
}

// File passed all checks. Time to move it to the destination folder.
else {
// First, change the file name, if needed.
if ($_UP['renomeia'] == true) {
    // File name with UNIX TIMESTAMP plus .jpg extension
    $nome_final = time().'.jpg';
} else {
    // Keep the original file name
    $nome_final = $_FILES['iMapIt_File']['name'];
}

// Check if it is possible to move the file to the destination folder
if (move_uploaded_file($_FILES['iMapIt_File']['tmp_name'], $_UP['pasta'] . $nome_final)) {
    // File successfuly uploaded
    echo "Upload successful!";
    } else {
        // It was not possible to upload the file, may be some issue with destination folder
        echo "It was not possible to upload the file, please try again";
    }
}
?>
---------------------------------------------------------------- End of PHP file (do not include this line)


Likewise, the HTML script titled "teste_upload.html" presented in the previous article to test the upload script was modified to send this new parameter. View the HTML source of the updated script below.

---------------------------------------------------------------- Beginning of HTML file (do not include this line)

<!DOCTYPE HTML>
<HTML>
    <HEAD>
            <TITLE>iMapIt Loader Test</TITLE>
            <META http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </HEAD>
    <BODY>
            <FORM id="frm" name="iMapIt_Form" action="imapit_uploader.php" method="POST" enctype="multipart/form-data">
                Select a file to upload<br>
                <INPUT id="iMapIt_File" name="iMapIt_File" type="file" style="width:100%;"/><br>
                Device ID: <INPUT id="DeviceId" name="DeviceId" type="text" style="width:40%;" /> <br>
                <INPUT id="submit" type="submit" value="Submit" />
                </FORM>
    </BODY>
</HTML>

---------------------------------------------------------------- End of HTML file (do not include this line)