Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery AJAX File Upload Error 500

So I'm trying to do a file upload using JQuery's AJAX thing, and it keeps giving me the error 500.

$(function() {
    $( 'form' ).submit
    (
        function()
        {
            $.ajax({
                type: 'POST',
                url: 'photochallenge/submit.php',
                data: new FormData(this),
                processData: false,
                contentType: false,
                success: function(data) {
                    Materialize.toast(data, 4000);
                }
            });
            return false;
        }
    );
});

I am also using this PHP code to process the file upload:

<?php
$target_dir = "uploads/";
$target_file = null;
$uploadOk = 1;
$response = "Please choose an image";
// Check if image file is a actual image or fake image
if(isset($_POST["pic"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        $uploadOk = 1;
    } else {
        $response = "File is not an image.";
        $uploadOk = 0;
    }


    // Check file size
    if ($uploadOk == 1 && $_FILES["fileToUpload"]["size"] > 500000) {
        $response = "Sorry, your file is too large.";
        $uploadOk = 0;
    }

    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) {

    // if everything is ok, try to upload file
    } else {
        //find target file
        $found = false
        $tmp = 0
        while(!$found)  {
            $target_file = $target_dir . $tmp . ".png";
            if(file_exists($target_file))   {
                $tmp = $tmp + 1;
            }   else    {
                $found = true;
            }
        }

        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
            $response = "Thank you for your submission!";
            shell_exec("python log.py ".$_POST["firstname"]." ".$_POST["lastname"]." ".$target_file);
        } else {
            $response = "Sorry, there was an error uploading your file.";
        }
    }
}

echo $response;
?>

Unfortunately, I cannot release the link to where the actual problem is, but hopefully this code is enough to help solve the problem. If any other details are needed, please do not hesitate to let me know.

like image 519
Wesley Soo-Hoo Avatar asked Nov 09 '22 14:11

Wesley Soo-Hoo


1 Answers

If you are not using ".htaccess", the issue may be in the shell_exec that may be crashing the application, if you are using something as fast-cgi.

Comment this line and perfom your script:

shell_exec("python log.py ".$_POST["firstname"]." ".$_POST["lastname"]." ".$target_file);

Using comments in PHP:

//shell_exec("python log.py ".$_POST["firstname"]." ".$_POST["lastname"]." ".$target_file);
like image 129
Guilherme Nascimento Avatar answered Nov 14 '22 21:11

Guilherme Nascimento