在ajax中发布文件在yii2中不起作用

时间:2017-05-17 05:48:07

标签: php jquery ajax yii2 yii2-basic-app

在我看来:

表格字段:

 echo $form->field($model, '['.$form_id.']['.$form_component.']c_data')->fileInput(['style' => 'color:black','data-admin'=>$admin_id,'data-form'=>$form_id,'date-component'=>$form_component,'data-unic'=>$unic_id,'data-label'=>$label,'data-user'=>$user_id,'data-required'=>$required,'data-comp_type'=>'9'])->label($label) ;

在我的按钮点击功能的相同视图页面脚本中:

 var site_url = '<?php echo Url::to (['formdata/movefile','id'=>'']); ?>' + unic ;

   var form = $(this).attr("data-form");
            var component = $(this).attr("date-component");
             var file_data = $('#formdata-'+ form + '-' + component + '-c_data').prop('files')[0];
                  var form_data = new FormData();                  
                  form_data.append('file', file_data);

                   $.ajax({
                url: site_url, // point to server-side PHP script 
                dataType: 'text',  // what to expect back from the PHP script, if anything
                cache: false,
                contentType: false,
                processData: false,
                data: form_data,                         
                type: 'post',
                success: function(php_script_response){

                }
     });

在我的控制器中:

 public function actionMovefile($id) {

                    $id= $id;


                $fileInfo = pathinfo($_FILES["file"]["name"]);
$ipath = "download/". $id . '.' .$_FILES["file"]["name"] ;


                if ( 0 < $_FILES['file']['error'] ) {
        echo 'Error: ' . $_FILES['file']['error'] . '<br>';
    }
    else {

       move_uploaded_file($_FILES["file"]["tmp_name"], $ipath);

    }


              }

脚本和文件没有错误..但我的上传文件未保存在文件夹

我已经检查了文件夹的路径, 我在basic-&gt; download

中创建了一个下载文件夹

我正在使用yii2的基本模板

请有人帮助我......

1 个答案:

答案 0 :(得分:0)

您是否检查过文件夹权限?

同时通过打印来检查$_FILES对象。