简体   繁体   English

使用JQuery和PHP进行Ajax上传

[英]Ajax Upload with JQuery and PHP

I have a problem with Ajax uploading using Jquery and PHP.. Although there're many stackoverflow posts discussing the same subject, I didn't find one that matches my case. 我有使用Jquery和PHP上传Ajax的问题​​。虽然有很多stackoverflow帖子讨论相同的主题,但我找不到一个与我的情况相符的帖子。

HTML : 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 : 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

<?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';
}

When I try the previous code, and upload a record file, it displays "failed 2", meaning that the "error" code is different from 0. 当我尝试上一个代码并上传一个记录文件时,它会显示“failed 2”,这意味着“error”代码与0不同。

when I var_dump the array $_FILES['uploadrecord']...this is what I get: 当我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

I can't figure out, why 'tmp_name' and type are set to empty strings, and why the size is set to 0. 我无法弄清楚,为什么'tmp_name'和type被设置为空字符串,以及为什么将size设置为0。

Where's the problem exactly? 究竟问题出在哪里? is it from the client or server side scripting? 是来自客户端还是服务器端的脚本?

Thanks in advance 提前致谢

ON MAC OSX: 在MAC OSX上:

1) Go to /etc/php.ini 1)转到/etc/php.ini
2) Open the file with permission to write/read 2)打开具有写入/读取 权限的文件
3) Search for line: 3)搜索行:

upload_max_filesize = 2M

4)Change to, for example, 6MB ou more: 4)例如,更改为6MB以上:

upload_max_filesize = 6M

5) Save and restart apache with command: 5)使用命令保存并重启apache:

sudo apachectl restart

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM