如何上传文件MVC php

时间:2016-08-21 05:11:10

标签: php

我有这段代码:

<div class='form-group'>
  <div style='background-color:#EFEFF4'>
    <div class='buttoncolumn' style='padding-top: 90px;padding-bottom: 5px; width:100%; background-color:#EFEFF4;'>
      <button id='disableGo' class='buttoncolumn' type='submit'  style=' background-color:#007AFF; filter: alpha(opacity=40); -moz-opacity: 0.4;opacity: 0.4;bottom:4%; margin-left:8%;position:absolute;border:none !important;' disabled> Save </button>
      <button id='go' class='buttoncolumn' type='submit'  style=' display:none;background-color:#007AFF;bottom:4%; margin-left:8%;position:absolute;border:none !important;'> Save </button>
    </div>
  </div>
</div>

<script type='text/javascript'>
  $(document).ready(function() {
    $('#TACForm').validate({
      rules: {streetnum: 'required',postalCode: 'required',},   
      highlight: function(element) { 
        $(element).closest('.form-group').addClass('has-error');
      },
      unhighlight: function(element) {
        $(element).closest('.form-group').removeClass('has-error'); 
      },
      errorElement: 'span', 
      errorClass: 'help-block', 
      errorPlacement: function(error, element) { 
        var found = $(element).closest('.checkbox');
        if(found.length) { 
          error.insertAfter(found); 
        } else { 
          error.insertAfter(element);
        }
      }, 
      submitHandler: function(form) {
        var paramsEncoded = $('form').serialize();    
        notificationRequest(paramsEncoded);
      }
    });
    function notificationRequest(params) 
    { 
      $.ajax({ type: 'post', 
              url: '/ium-tac/', 
              data: params, contentType: 'application/x-www-form-urlencoded;charset=UTF-8', 
              success: function(response) { 
                registered(true);   
                console.log(response); 
              }, 
              error: function(request, errorType, errorMessage) {
                console.log('Error ' + errorType + ' : ' + errorMessage);
              }
             });
    }
</script>

来自http://www.elvishsu.com/2014/01/start-your-own-mvc-framework-with-php.html#.V7lBVnUrLxs

在这种情况下,请任何人都可以告诉我如何处理文件上传 我的意思是我应该如何处理:

     // assign params by methods  
       switch($method){
        case "GET": // view
            // we need to remove _route in the $_GET params
            unset($_GET['_route']);
            // merege the params
            $this->params_ = array_merge($this->params_, $_GET);               
        break;
        case "POST": // create
        case "PUT":  // update
        case "DELETE": // delete
        {
            // ignore the file upload
            if(!array_key_exists('HTTP_X_FILE_NAME',$_SERVER))
            {
                if($method == "POST"){
                    $this->params_ = array_merge($this->params_,      $_POST); 
                }else{           
                    // temp params 
                    $p = array();
                    // the request payload
                    $content = file_get_contents("php://input");
                    // parse the content string to check we have [data] field or not
                    parse_str($content, $p);
                    // if we have data field
                    $p = json_decode($content, true);
                    // merge the data to existing params
                    $this->params_ = array_merge($this->params_, $p);
                }   
            }          
        }
        break;                
    }

由于

1 个答案:

答案 0 :(得分:0)

这是我解决的问题:

// assign params by methods  
    switch($method){
        case "GET": // view
            // we need to remove _route in the $_GET params
            unset($_GET['_route']);
            // merege the params
            $this->params_ = array_merge($this->params_, $_GET);               
        break;
        case "POST": // create
        case "PUT":  // update
        case "DELETE": // delete
        {

                if($method == "POST" && isset($_FILES)){
                    if (!empty($_FILES))
                    {
                        $this->params_ = array_merge($this->params_, $_POST);
                        $this->params_ = array_merge($this->params_, $_FILES);                            
                    }else{
                        $this->params_ = array_merge($this->params_, $_POST); 
                    }
                }else{           
                    // temp params 
                    $p = array();
                    // the request payload
                    $content = file_get_contents("php://input");
                    // parse the content string to check we have [data] field or not
                    parse_str($content, $p);
                    // if we have data field
                    $p = json_decode($content, true);
                    // merge the data to existing params
                    $this->params_ = array_merge($this->params_, $p);
                }   

        }
        break;                
    }

我把它放在这里,以防任何人也试图从该教程构建MVC并发现很难上传 谢谢大家