0

我在使用 Jquery 和 PHP 上传 Ajax 时遇到问题。虽然有很多讨论同一主题的 stackoverflow 帖子,但我没有找到与我的情况相匹配的帖子。

HTML:

<form action="uploadrecord.php" id="upload_record_form" method="post" enctype="multipart/form-data">
               <div id="upload_record_form_result"></div>
                <ul>
                    <li><input type="file" name="uploadrecord" id="uploadrecord"/></li>
                </ul>
                 <progress></progress>
            </form>

        </div>

Javascript:

 $('#uploadrecord').live('change', function(event) {

        var formData = new FormData($('#upload_record_form')[0]);

        $.ajax({
            url: $('#upload_record_form').attr('action'),
            type: 'POST',
            enctype: 'multipart/form-data',
            xhr: function() {  
                myXhr = $.ajaxSettings.xhr();
                if (myXhr.upload) { 
                    myXhr.upload.addEventListener('progress', progressHandlingFunction, false); 
                }
                return myXhr;
            },

            success: completeHandler = function(data) {
                $('#upload_record_form_result').html(data);
            },
            error: errorHandler = function() {
                alert("Failure");
            },
            data: formData,
            cache: false,
            contentType: false,
            processData: false
        }, 'json');


    });

PHP

<?php
if (isset($_FILES['uploadrecord'])) {
    if ((!empty($_FILES["uploadrecord"])) && ($_FILES['uploadrecord']['error'] == 0)) {

        $filename = basename($_FILES['uploadrecord']['name']);
        $ext = substr($filename, strrpos($filename, '.') + 1);

        $types = array(
            'video/mp4', 'video/3gpp', 'video/divx', 'video/flv', 'video/mpeg', 'video/mpeg-2', 'video/mpeg4', 'video/mpeg4'
        );

        $type = $_FILES['uploadrecord']['type'];
        if (in_array($type, $types) && $_FILES["uploadrecord"]["size"] < 20000000) {

            $new = sha1(date('Y/m/d H:i:s'));
            $newname = dirname(__FILE__) . '/records/' . $new . '.' . $ext;

            if ((move_uploaded_file($_FILES['uploadrecord']['tmp_name'], $newname))) {
                echo 'done';
            } else {
                echo 'failed 1';
            }
        }
   } else {
       echo 'failed 2';
    }
} else {
    echo 'Upload failed 3';
}

当我尝试前面的代码,并上传一个记录文件时,它显示“失败2”,这意味着“错误”代码与0不同。

当我 var_dump 数组 $_FILES['uploadrecord']...这就是我得到的:

array (size=5)
  'name' => string 'Nao Robot.flv' (length=13)
  'type' => string '' (length=0)
  'tmp_name' => string '' (length=0)
  'error' => int 1
  'size' => int 0

我想不通,为什么 'tmp_name' 和 type 设置为空字符串,以及为什么 size 设置为 0。

问题究竟出在哪里?是来自客户端还是服务器端脚本?

提前致谢

4

1 回答 1

2

在 MAC OSX 上:

1)转到/etc/php.ini
2)打开具有写/读权限的文件 3)搜索行:

upload_max_filesize = 2M

4)更改为,例如,6MB 或更多:

upload_max_filesize = 6M

5)使用命令保存并重新启动apache:

sudo apachectl restart
于 2015-11-09T14:04:56.127 回答