evv_gis evv_gis - 3 months ago 22
jQuery Question

Issue uploading kml file to remote directory hosted by GoDaddy

I have looked into numerous postings regarding issues uploading files to a remote directory (hosted via GoDaddy; windows machine), but have been unsuccessful implementing the changes.

Here is what I am trying to do. I have written a very simple PHP to upload a local KML file to a remote GoDaddy directory. I am doing this to create a public-facing URL for the KML, which then makes it possible to add it to my Google Map application.

Things I have done:


  • Ensured the permissions allow "Write" to the directory I am uploading to.

  • Tested the PHP by uploading to a directory on my website. This works, but the website is not public facing (so it will not overlay the KML on the map, hence why I am trying to upload to GoDaddy now).

  • Rewritten my AJAX post function to use different dataTypes (apparently this has been a fix for other GoDaddy users when uploading files)

  • Added the .kml MIME type to the GoDaddy web.config



What does work:


  • If I use an FTP file manager, and manually place a KML in the directory, it is accessible and loads on my map just fine.

  • Apparently the PHP works because my .done() function fires after attempting to upload.



What my limits are:


  • I cannot log into the Account Manager of GoDaddy because the only person with this information is our webmaster (who coincidentally is out of the office).

  • PHP, I have never done anything with it other than read tutorials, create a Hello World page, and a mail form.



Here is the AJAX post:

$.ajax({
url: 'http://www.example.com/dev/Drawings/upload.php',
type: 'POST',
processData: false,
data: data,
dataType: 'html'
})
.done(function () {
dKML = new google.maps.KmlLayer('http://www.example.com/dev/Drawings/Uploads/' + filename);
dKML.setMap(map);
})
.fail(function (xhr, errorType, exception) {
var errorMessage = exception + "\n" + xhr.status + "\n" + errorType;
alert(errorMessage);
});


Here is my PHP (upload.php):

<?php
header("Access-Control-Allow-Origin: *");
if ( !isset($_FILES['file'])) {
die();
} else {
$temp_file = $_FILES['file']['tmp_name'];
move_uploaded_file($temp_file, "/Uploads/" . $_FILES['file']['name']);
}

?>


I have been hesitant to post on here because I was sure I could figure this out but honestly I am lost from the countless hours of trial and error.

My questions are:

Does anything appear incorrect in terms of syntax?

Have any of you experienced a similar issue, and if so, what were you able to do to address the problem?

Is it possible that the issue exists because when testing the upload I am using localhost?

Thank you in advance!

Answer

Once I discovered the tmp_name value was null, I started looking for better error handling to add to my PHP.

I ended up changing my PHP to:

`code

<?php
    $error = "";
    $msg = "";
    $fileElementName = 'file';
    if(!empty($_FILES[$fileElementName]['error']))
    {
        switch($_FILES[$fileElementName]['error'])
        {

        case '1':
            $error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
            break;
        case '2':
            $error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
            break;
        case '3':
            $error = 'The uploaded file was only partially uploaded';
            break;
        case '4':
            $error = 'No file was uploaded.';
            break;
        case '6':
            $error = 'Missing a temporary folder';
            break;
        case '7':
            $error = 'Failed to write file to disk';
            break;
        case '8':
            $error = 'File upload stopped by extension';
            break;
        case '999':
        default:
            $error = 'No error code available';
    }
}elseif(empty($_FILES['file']['tmp_name']) || $_FILES['file']['tmp_name'] == 'none')
{
    $error = 'No file was uploaded..';
}else 
{
        $msg .= " File Name: " . $_FILES['file']['name'] . ", ";
        $msg .= " File Size: " . @filesize($_FILES['file']['tmp_name']);
        //for security reason, we force to remove all uploaded file
        @unlink($_FILES['file']);       
}       
echo "{";
echo                "error: '" . $error . "',\n";
echo                "msg: '" . $msg . "'\n";
echo "}";
?>

`

I also updated the .submit() function on my form to:

`code

e.preventDefault();
var dataString;
var action = 'http://www.example.com/dev/Drawings/upload.php';
if ($("#form").attr("enctype") == "multipart/form-data") {
    dataString = new FormData($("#form").get(0));
    contentType = false;
    processData = false;
}
$.ajax({
    type: "POST",
    url: action,
    data: dataString,
    dataType: "html", 
    contentType: contentType,
    processData: processData
})
.done(function () {
    delay(function () {
        dKML = new google.maps.KmlLayer('http://www.example.com/dev/Drawings/Uploads/' + filename);
        dKML.setMap(map);
    }, 2000);
})
.fail(function (xhr, errorType, exception) {
    var errorMessage = exception + "\n" + xhr.status + "\n" + errorType;
    alert(errorMessage);
});

`

Once I was able to resolve the $_FILES variable in the PHP, everything started to work as expected.