我无法使用ajax请求获取$ _FILE ['file'] ['name']值

时间:2013-12-09 15:24:31

标签: php ajax

我是html表单。现在我正在尝试使用带有ajax请求的php上传文件。但是当我通过带有ajax请求的php上传文件时,我收到以下错误消息。这是我的ajax请求问题还是其他什么?

Notice: Undefined index: file in D:\Software Installed\xampp\htdocs\wix\users\insert_galary.php on line 16

html表单:

    <p><a href="javascript:void(null);" onclick="showDialog2();">Add more</a></p>                
    <div id="dialog-modal2" title="Upload your galary image" style="display: none;">
    <form method="post" action="insert_galary.php" id="myForm2" enctype="multipart/form-data" >
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
    <td>Upload image</td>
    <td><input type="file" name="file" class="tr"/></td>
    </tr>
    </table>
    </form> 

    <span id="result2"></span>
    <script type="text/javascript">
    $("#sub2").click( function() {
    $.post( $("#myForm2").attr("action"), 
    $("#myForm2 :input").serializeArray(), 
    function(info){ $("#result2").html(info); 
    });
    clearInput();
    });

    $("#myForm2").submit( function() {
    return false;   
    });
    </script>
    </div>

        function showDialog2()
        {
            $("#dialog-modal2").dialog(
            {
                width: 610,
                height: 350,
                open: function(event, ui)
                {
                    var textarea = $('<textarea style="height: 276px;">');
                    $(textarea).redactor({
                        focus: true,
                        autoresize: false,
                        initCallback: function()
                        {
                            this.set('<p>Lorem...</p>');
                        }
                    });
                }
             });
        }

Php表格:

$gid = mt_rand(100000, 999999); 
            $file =  $_FILES["file"]["name"];
            $type =  $_FILES["file"]["type"];
            $size =  ($_FILES["file"]["size"] / 1024);
            $temp =  $_FILES["file"]["tmp_name"];   
            $allowedExts = array("gif", "jpeg", "jpg", "png");
            $temp = explode(".", $_FILES["file"]["name"]);
            $extension = end($temp);    
            $galary_pic = $gid.".".$extension;  
            $galary_directory = "../galary_images/";

            $err =  array();

            if(isset($file))
            {
                    if(empty($file))                    
                        $err[] = "Upload your picture";             
                    elseif(!in_array($extension, $allowedExts))
                        $err[] = "Uploaded file must be gif, jpeg, jpg, png format";
                    elseif($size > 500)
                        $err[] = "Uploaded file must be within 500kb";                                      
            }

            if(!empty($err))
            {
                echo "<div class='error'>"; 
                foreach($err as $er)
                {
                    echo "<font color=red>$er.</font><br/>";                
                }
                echo "</div>";
                echo "<br/>";
            }
            else
            {

                echo "sql query";               

            }

注意:我已在上一天提交此问题,但我无法得到任何正确答案。对不起它

1 个答案:

答案 0 :(得分:0)

您没有上传文件,只是在没有附加任何文件的情况下发出POST请求。如果您正在寻找ajax文件上传器,请尝试在此处上传jquery文件

http://blueimp.github.io/jQuery-File-Upload