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

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

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法