dongshi1914 2015-05-03 05:50
浏览 631
已采纳

JQuery AJAX文件上传错误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.

  • 写回答

1条回答 默认 最新

  • douwen2158 2015-05-03 06:58
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?