php ajax文件上传,$ _FILE为空

时间:2017-07-15 06:54:32

标签: javascript php jquery ajax

我有一个blob对象,有些数据需要发布到fileHandler.php

所以我将它们打包成FormData:

        console.log("start saving");
        var url1 = URL.createObjectURL(blob);
        var url2 = "data:application/octet-stream," + encodeURIComponent(JSON.stringify(dataPack));
        console.log(url1);
        console.log(url2);
        fd.append('realName', dataPack.name);
        fd.append("ans", JSON.stringify(dataPack.ans));
        fd.append("log", JSON.stringify(dataPack.log));
        fd.append("part", dataPack.part);
        fd.append('fileToUpload', blob);
        window.postData = fd;

并通过ajax上传它们:

    $.ajax({
        type: 'POST',
        url: '../php/fileHandler.php',
        data: postData,
        processData: false,
        contentType: false,
        success: function() {
            uploadSuccess();
        },
        error: function() {
            uploadFail();
        },
        progress: function(e) {
            console.log(e);
            //make sure we can compute the length
            if(e.lengthComputable) {
                //calculate the percentage loaded
                var pct = parseInt((e.loaded / e.total) * 100);

                //log percentage loaded
                $('#uploadProgress').width(pct+"%").text(pct+"%");
                console.log(pct);
            }
            //this usually happens when Content-Length isn't set
            else {
                console.warn('Content Length not reported!');
            }
        }
    }).done(function(data) {
        console.log(data);
        if(data ==="ok") {
            uploadSuccess();
        }
        else {
            uploadFail();
        }
    });

php文件处理程序:

<?php
$target_dir = "uploads/";
$realName = trim($_POST['realName']);
$part = $_POST['part'];
$ans = json_decode($_POST['ans']);
$log = json_decode($_POST['log']);
$fileNameNoEx = $target_dir . $realName . "-" . $part ."-". time();
$target_file = $fileNameNoEx . ".mp3";
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";

// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        //save files
        $f = fopen($fileNameNoEx.".txt", "w");
        fwrite($f, "Answer log:\n");
        foreach ($ans as $page => $val) {
            fwrite($f, "$page : $val\n");
        }
        fwrite($f, "\nPage switching event log:\n");
        foreach ($log as $l) {
            fwrite($f, "{$l->time}s  --->  {$l->page}\n");
        }
        fclose($f);
        echo "ok";
    } else {
        var_dump($ans);
        var_dump($log);
        var_dump($_POST);
        var_dump($_FILES);
        var_dump($target_file);
        echo "asdsad";
        echo "Sorry, there was an error uploading your file.";
    }
}
然后奇怪的事情发生了,有时上传失败了,当我再次尝试上传时(在控制台中,使用相同的数据),浏览器继续记录:

NULL
NULL
array(0) {
}
array(0) {
}
string(24) "uploads/--1500100885.mp3"
asdsadSorry, there was an error uploading your file.

似乎postData是空的?但是当我在浏览器控制台中检查postData时:

> postData.get("fileToUpload")
File {name: "blob", lastModified: 1500101804839, lastModifiedDate: Sat Jul 15 2017 14:56:44 GMT+0800 (China Standard Time), webkitRelativePath: "", size: 12597888…}
> postData.get("realName")
"jjj"

为什么???? $_FILES[]$_POST[]如何发送?

当文件非常大时,通常会发生此问题。

1 个答案:

答案 0 :(得分:1)

PHP有一个最大帖子大小,尝试增加它:Increasing the maximum post size

如果PHP没有此限制,我可以向您的Web服务器发送一个无限大的POST,直到它用完磁盘或RAM。