通过ajax将文件发送到PHP时未定义的索引

时间:2015-02-25 12:48:31

标签: javascript php jquery ajax

嘿,所以我正在处理文件上传表单,一切正常但PHP文件

HTML:

<h2>Upload a new video</h2>
<p>THEN SET UP THE BASIC INFORMATION</p>

<form class="upf" method="post" enctype="multipart/form-data">
    <h1><i class="icono-upArrow"></i></h1>
    <input type="file" name="vid" id="video" />
</form> 

JQuery:

$.fn.upload = function(remote,data,successFn,progressFn) {
    // if we dont have post data, move it along
    if(typeof data != "object") {
        progressFn = successFn;
        successFn = data;
    }
    return this.each(function() {
        if($(this)[0].files[0]) {
            var formData = new FormData();
            formData.append($(this).attr("name"), $(this)[0].files[0]);

            // if we have post data too
            if(typeof data == "object") {
                for(var i in data) {
                    formData.append(i,data[i]);
                }
            }

            // do the ajax request
            $.ajax({
                url: remote,
                type: 'POST',
                xhr: function() {
                    myXhr = $.ajaxSettings.xhr();
                    if(myXhr.upload && progressFn){
                        myXhr.upload.addEventListener('progress',function(prog) {
                            var value = ~~((prog.loaded / prog.total) * 100);

                            // if we passed a progress function
                            if(progressFn && typeof progressFn == "function") {
                                progressFn(prog,value);

                            // if we passed a progress element
                            } else if (progressFn) {
                                $(progressFn).val(value);
                            }
                        }, false);
                    }
                    return myXhr;
                },
                data: formData,
                dataType: "json",
                cache: false,
                contentType: false,
                processData: false,
                complete : function(res) {
                    var json;
                    try {
                        json = JSON.parse(res.responseText);
                    } catch(e) {
                        json = res.responseText;
                    }
                    if(successFn) successFn(json);
                }
            });
        }
    });
};


$("#video").change(function(){
    $("#video").upload("includes/upload_vid.php", function(suess){
        $.ajax({
            url: 'includes/upload_vid.php',
            success:function(data){
                $(".tesst").html(data);
            }
        });
    },$("#prog") );
});

现在JQuery正常工作,我在进度条中获得了进展 但是我认为PHP没有做到这一点,因为我在AJAXing文件的方式我检查PHP中的文件信息是错误的任何方式这里是PHP代码:

<?php
    $video_err ="";

    if($_FILES['vid']['type']=='video/mp4'){ 
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1023456789";

        $randName = substr(str_shuffle($chars), 0 , 15);

        if(file_exists('data/users/videos/'.$randName.''.$_FILES['vid']['name'])){

        }// end of file exits
        else {
            $des = 'data/users/videos/'.$randName.''.$_FILES['vid']['name'];
            if(move_uploaded_file($_FILES['vid']['tmp_name'], $des)){
                $video_err.= "Done";
            } else{
                $video_err .= "Sorry something went wrong, please try later";
            }
        }
    }// end of checking the file type[video]
    else{
        $video_err.= "Invaild File, sorry we now only accept .mp4 format";
    }
    echo $video_err;    

整个问题是$_FILES['vid']是未定义的,而我做了ajax文件,那该怎么办?

1 个答案:

答案 0 :(得分:0)

你可以使用:

$("#video").change(function(){
    $("#video").upload(
       "includes/upload_vid.php", 
       function(success){
           $(".tesst").html(success);
       },
       $("#prog") 
    );
});