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 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题