$ _SERVER ["CONTENT_LENGTH"]在使用XmlHttpRequest上传文件时返回零

Sam*_*Sam 9 php jquery xmlhttprequest

我在我的开发机器上遇到了一个问题,这台机器看起来很孤立,我无法弄明白.我有一个jQuery文件上传器,它将用户选择的文件发布到PHP脚本,以便使用XmlHttpRequest进行处理.该脚本在运行OSX10.6.3和MacAMP 1.9的MacBook Pro上工作正常,但是在我的iMac上使用完全相同的操作系统和MAMP版本,具有相同的服务器映像,它失败了.

我已经将错误的原因追溯到$_SERVER["CONTENT_LENGTH"]返回0, even though I can get the filename just fine and everything else seems to have succeeded about the request. For some reason it just won't seem to give me the actual content length. Here is the code that is causing the problem - the function in question is getSize().

class qqUploadedFileXhr {
    /**
     * Save the file to the specified path
     * @return boolean TRUE on success
     */
    function save($path) {    
        $input = fopen("php://input", "r");
        $temp = tmpfile();
        $realSize = stream_copy_to_stream($input, $temp);
        fclose($input);

        if ($realSize != $this->getSize()){            
            return false;
        }

        $target = fopen($path, "w");        
        fseek($temp, 0, SEEK_SET);
        stream_copy_to_stream($temp, $target);
        fclose($target);

        return true;
    }
    function getName() {
        return $_GET['qqfile'];
    }
    function getSize() {
        if (isset($_SERVER["CONTENT_LENGTH"])){
            return (int)$_SERVER["CONTENT_LENGTH"]; //*THIS* is returning 0            
        } else {
            throw new Exception('Getting content length is not supported.');
        }      
    }   
}
Run Code Online (Sandbox Code Playgroud)

Sam*_*Sam 6

解决了!似乎我使用的jQuery脚本在firefox 3.5.x下失败,我更新到3.6.9并且它工作正常.

现在我必须找到一些方法使它向后兼容旧版本的Firefox.


Yan*_*hon 3

您是否将编码类型设置为multipart/form-data

<form action="upload.php" method="post" enctype="multipart/form-data">
  ...
  <input type="file" ... />
  ...
</form>
Run Code Online (Sandbox Code Playgroud)