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



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

Notice: Undefined index: file in D:Software Installedxampphtdocswixusersinsert_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";               
            }

注意:我昨天已经提交了这个问题,但我没有得到任何正确的答案。很抱歉

您不是在上传文件,而是在不附加任何文件的情况下发出POST请求。如果你正在寻找ajax文件上传器,请尝试在这里上传jquery文件

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

相关内容

  • 没有找到相关文章

最新更新