$ _FILES数组为空

时间:2012-03-19 20:06:50

标签: php file

这是我第一次尝试上传文件,出于某种原因我的$ _FILES数组为空。我一遍又一遍地检查我的HTML,看起来对我来说没问题。当我尝试在eclipse中调试下面的代码时,$ _Files数组在我检查时是空的。我检查了它的php_ini文件:

  • file_uploads = On
  • upload_max_filesize = 2M

表单位于fancybox模式窗口中。 我的HTML看起来像这样:

<form  action="/CiREM/attachments/addAttachmentsModal.php?requestId=120" enctype="multipart/form-data" method="post" id="addattachment" name="addattachment" class="form-vertical" autocomplete="off">
        <input type='hidden' id='requestId' name='requestId' value="120"/>
        <input type='hidden' id='listScreen' name='listScreen' value=""?>   
        <input type='hidden' name='MAX_FILE_SIZE' value='4000000' /><br/> <strong>Max File size Allowed: </strong>4 Mb <br/><strong>File Formats Allowed: </strong>gif,jpeg,jpg,png<br/><hr/>               <div class="control-group">


                <div class="controls input">
                    <input class="input-file" type="file" name="upload_file[]" id="upload_file[]"/><br/>
                </div>
                </div>
                    <input class="input-file" type="file" name="upload_file[]" id="upload_file[]"/><br/>
                </div>
                </div>
                    </div>
                    <div class ="clear"></div>
                        <input id="addAttachmentsBtn" type="submit" class="btn btn-primary btn-large" value="Add Attachments"/>
                    </form>         

我的php是

<form  action="<?php echo $_SERVER['PHP_SELF']."?requestId=".$requestId?>" enctype="multipart/form-data" method="post" id="addattachment" name="addattachment" class="form-vertical" autocomplete="off">
        <input type='hidden' id='requestId' name='requestId' value="<?php echo $requestId;?>"/>
        <input type='hidden' id='listScreen' name='listScreen' value="<?php echo $listScreen;?>"?>  
        <?php 
        if ($CIREM['MAX_IMG_NUM']>0){
            echo "<input type='hidden' name='MAX_FILE_SIZE' value='".$CIREM['MAX_IMG_SIZE']."' />";
            echo "<br/> <strong>Max File size Allowed: </strong>".($CIREM['MAX_IMG_SIZE']/1000000)." Mb <br/><strong>File Formats Allowed: </strong>".$CIREM['IMG_TYPES']."<br/><hr/>";?>

        <?php   for ($i=1;$i<=$CIREM['MAX_IMG_NUM'];$i++){?>


                <div class="controls input">
                    <input class="input-file" type="file" name="upload_file[]" id="upload_file[]"/><br/>
                </div>

            <?php }?>

        <?php }
        else{
            echo "<p class='alert alert-info'>Attachment uploading is not allowed</p>";
        }
        ?>

非常感谢任何帮助。

谢谢, 夏奇拉

2 个答案:

答案 0 :(得分:0)

在不查看意大利面条代码的情况下,首先检查您的$ _POST是否也为空。

如果是,请确保post_max_size大于upload_max_filesize。这两个设置必须一致。

答案 1 :(得分:0)

首先对于迟到的回复感到抱歉,但是因为有时候人们偶然发现了stackoverflow上的旧主题,所以我决定写下它对我有用的方式。

因此,要通过ajax发送文件,您必须使用FormData。

我会粘贴一个github项目的链接,我必须通过ajax提交表单,这样你就可以查看工作示例并在此处粘贴代码片段。

链接:https://github.com/pihh/auto-ajax-form

代码:

$( "form" ).on('submit',function( event ) {
        if($(this).attr('ajax')){
            event.preventDefault();
            var marianaFormUrl = $(this).attr('action');
            var marianaFormId = $(this).attr('id');
            var marianaFormMethod = $(this).attr('type');
            var marianaFormSucess = $(this).attr('success');
            var marianaFormComplete = $(this).attr('complete');
            var marianaFormBefore = $(this).attr('before');
            var marianaFormInputs = $('#' + marianaFormId +' :input');
            var marianaEncType = $(this).attr('enctype');
            var marianaFormData = {};

            // Set enctype
            if(marianaEncType === undefined || marianaEncType == ''){
                $(this).attr('enctype','multipart/form-data');
            }

            // Run Ajax Call
            $.ajax({
                url: marianaFormUrl,
                type: marianaFormMethod,
                dataType: 'JSON',
                data: new FormData( this ),
                processData: false,
                contentType: false,
                cache: false,
                success:function(data){
                    // Run success
                    if(marianaFormSucess !== undefined && marianaFormSucess !== ''){
                        var fn = marianaFormSucess;
                        var func = fn +'( data )';
                        eval(func);
                    }
                },
                complete:function(data){
                    // Run complete
                    if(marianaFormComplete !== undefined && marianaFormComplete !== ''){
                        var fn = marianaFormComplete;
                        var func = fn +'( data )';
                        eval(func);
                    }
                }
            });
        }
    });

就是这样,使用formData可以很好地发送文件。