0

我正在使用 ajax 表单来动态显示包含文件上传选项的表单。我正在获取每个文本值的 post 值,但 File 数组为空。我已执行print_r($_POST)print_r($_FILES)查看发布数据,$_FILES 数组显示空值。

这是我正在使用的表格。

<form class="form-3 form-horizontal ajxfrm" id="step-three" enctype="multipart/form-data" action="<?php echo $html->addLink(array('controller'=>'homes','action'=>'step_three_ajax')); ?>" method="post">
<div id="calendar">
    <div class="clear"></div>
    <div style="position:relative">
        <div class="control-group">
            <div class="control-label"><label class="control-label">Name</label></div>
            <div class="controls"><input id="name" type="text" name="name" /></div>
        </div>
    </div>
    <div style="position:relative">
        <div class="control-group">
            <div class="control-label">Email</div>
            <div class="controls"><input id="email" type="text" name="email" /></div>
        </div>
    </div>
    <div style="position:relative">
        <div class="control-group">
            <div class="control-label">Contact</div>
            <div class="controls"><input id="contact" type="text" name="contact" /></div>
        </div>
    </div>
    <div style="position:relative">
        <div class="control-group">
            <div class="control-label">Files<br/><span style="font-size:10px; font-style:italic">(Optional)</span></div>
            <div class="controls">
                <input id="fileupload" type="file" name="fileupload" /><br/>
            </div>
        </div>
    </div>
    <div style="position:relative">
        <div class="control-group">
            <div class="control-label">Your Message</div>
            <div class="controls"><textarea id="message" rows="3" name="message"></textarea></div>
        </div>
    </div>
    <div class="clear"></div>
</div>
<div id="submit">       
    <input type="hidden" name="post" value="1"/>
    <input type="submit" class="btn green" value="Next"/> 
</div>
</form>

这是ajax调用

$('.ajxfrm').live('submit',function(){
    var thisObj = this;
    var submit = true;
    if('step-two'==$(thisObj).attr('id')){
        submit = stepTwo();
    }else if('step-three'==$(thisObj).attr('id')){
        submit = stepThree();
    }           
    if(submit){
        $.ajax({
            type: "POST",
            data: $(thisObj).serialize(),
            url: $(thisObj).attr('action'),
            beforeSend: function(){
                showOverLayer();
            },
            success: function(data){
                $(thisObj).parent("#content").empty().html(data).hide().fadeIn(function(){
                    setBlocks();
                    setLiClick();
                    hideOverLayer();
                });
                changeCalNaviHref();
            }
        });
    }
    return false;
});

这里我使用 $booker->uploadfiles(); 调用上传函数;在控制器这里是控制器方法功能

public function step_three(){
    //save booking
    if(isset($_POST['post'])){
        $booker = new Booker();     
        //create booker
        $_SESSION['userdat']['name']=$_POST['name'];
        $_SESSION['userdat']['email']=$_POST['email'];
        $_SESSION['userdat']['contact']=$_POST['contact'];
        $_SESSION['userdat']['message']=$_POST['message'];

        if($booker->setsessions($_SESSION['userdat']))
        {
            $booker->uploadfiles();
            $this->redirect(array('controller'=>'homes','action'=>'step_four'));
        }
        else
        {
            $this->setFlashMsg('Please Provide Valid Information','message-box ok');
        }
    }
}

这是模型函数uploadfiles()

public function uploadfiles()
    {
    if(isset($_FILES['fileupload']))
    {
        $errors= array();
        foreach($_FILES['fileupload']['tmp_name'] as $key => $tmp_name )
        {
            print_r($_FILES);
            exit;
            $file_name = $key.$_FILES['fileupload']['name'][$key];
            $file_size =$_FILES['fileupload']['size'][$key];
            $file_tmp =$_FILES['fileupload']['tmp_name'][$key];
            $file_type=$_FILES['fileupload']['type'][$key]; 
            if($file_size > 2097152)
            {
                $errors[]='File size must be less than 2 MB';
            }   
            $upload_dir = ROOT;
            $desired_dir="/uploads/";
            if(empty($errors)==true)
            {
                if(is_dir($desired_dir)==false)
                {
                    mkdir("$desired_dir", 0700); // Create directory if it does not exist
                }
            if(is_dir("$desired_dir/".$file_name)==false)
            {
                //$file_path=$upload_dir."$desired_dir/".$file_name;
                move_uploaded_file($file_tmp,$upload_dir."$desired_dir/".$file_name);
            }
            else
            {                                           // rename the file if another one exist
                $file_path=$upload_dir."$desired_dir/".$file_name.time();
                rename($file_tmp,$file_path) ;              
            }           
        }
        else
        {
            print_r($errors);
        }
    }
    if(empty($error))
    {
            //echo "Success"; exit;
            return true;
    }
    return false;
}

我在这里想念什么?

4

3 回答 3

1

Try this.

$('.ajxfrm').live('submit',function(){
        var thisObj = $(this),
            submit = true;

        if('step-two'== thisObj.attr('id')){
            submit = stepTwo();
        }else if('step-three'== thisObj.attr('id')){
            submit = stepThree();
        }
        if(submit){
            $.ajax({
              type: "POST",
              data: thisObj.serialize(),
              url: thisObj.attr('action'),
              beforeSend: function(){
                 showOverLayer();
              },
              success: function(data){
                 thisObj.parent("#content").empty().html(data).hide().fadeIn(function(){
                     setBlocks();
                     setLiClick();
                     hideOverLayer();
                 });
                 changeCalNaviHref();
              }
            });
        }
    return false;
});

Can you show me one console.log(data) on the success method?

Thanks

于 2013-07-23T17:00:38.277 回答
0

这里总共有两个远射:

1)您提到您的会话代码是“最高的”。你在用那里的东西session_regenerate_id吗?这将与并行上传混淆。

或者

2)查看当其他会话进入时您的会话文件是否被锁定。在 .google 上搜索一两篇文章session_write_close()

但它更有可能是#1。

于 2013-07-23T17:52:32.343 回答
0

您不能使用“旧”AJAX 发送文件(不是这种方式)。

如果要发送文件,请查看 XMLHttpRequest2。很好的教程在这里 - http://www.html5rocks.com/en/tutorials/file/xhr2/

于 2013-07-26T11:57:05.783 回答