使用PHP54中的会话使用codeigniter上传进度条

use*_*153 16 javascript php session codeigniter

参考本教程.使用php跟踪上传进度.我想让它在Codeigniter中运行.我没有意识到开始让它在CI中发挥作用.我想上传文件并跟踪进度.

在我的CI视图中

 <?php $arr = array("id"=>"myform");
       echo form_open_multipart("welcome/uploads",$arr); ?>
      <input type="hidden" value="myForm" name="<?php echo ini_get("session.upload_progress.name"); ?>">
      <table>
      <tr>
          <td>file</td>
         <td><input type="file" name="images[]" multiple></td>
     </tr>
     <tr>
         <td>name</td>
         <td><input type="text" name="naam"></td>
    </tr>
    <tr>
        <td></td>
        <td><input type="submit"></td>
    </tr>
</table>
<?php echo form_close(); ?>
<div id="bar_blank">
Run Code Online (Sandbox Code Playgroud)

脚本

function toggleBarVisibility() {
  var e = document.getElementById("bar_blank");
  e.style.display = (e.style.display == "block") ? "none" : "block";
}

function createRequestObject() {
   var http;
   if (navigator.appName == "Microsoft Internet Explorer") {
       http = new ActiveXObject("Microsoft.XMLHTTP");
   }
   else {
      http = new XMLHttpRequest();
  }
     return http;
}

 function sendRequest() {
   var http = createRequestObject();
   http.open("GET", "<?php echo base_url().'index.php/welcome/progress' ?>");
   http.onreadystatechange = function () { handleResponse(http); };
   http.send(null);
 }

function handleResponse(http) {
   var response;
   if (http.readyState == 4) {
    response = http.responseText;
    document.getElementById("bar_color").style.width = response + "%";
    document.getElementById("status").innerHTML = response + "%";

    if (response < 100) {
        setTimeout("sendRequest()", 1000);
    }
    else {
        toggleBarVisibility();
        document.getElementById("status").innerHTML = "Done.";
      }
   }
}

 function startUpload() {
     toggleBarVisibility();
    setTimeout("sendRequest()", 1000);
 }

 (function () {
      document.getElementById("myForm").onsubmit = startUpload;  //error is here
})();
Run Code Online (Sandbox Code Playgroud)

根据上面的教程,它在核心php.向控制器提交表单CI请求时dashboard/addRoom,我的页面无论如何都会刷新.但是在教程中,Form重定向到PHP_SELF(相同的php文件).我对它没有任何想法.请帮我.

调节器

 function progress()
    {
        session_start();

        $key = ini_get("session.upload_progress.prefix") . "myForm";
        if (!empty($_SESSION[$key])) {
            $current = $_SESSION[$key]["bytes_processed"];
            $total = $_SESSION[$key]["content_length"];
            echo $current < $total ? ceil($current / $total * 100) : 100;
        }
        else {
            echo 100;
        }
    }
    public function uploads()
    {
        if(!empty($_FILES['images']['name'][0]))
       {
        //uploadinf file code
       }
    }
Run Code Online (Sandbox Code Playgroud)

jog*_*_pi 5

如果你只想用jQuery做,那么我已经使用了jQuery Form Submit Plugin,它将为你提供进展等等.正如您在评论中所说,您不需要任何插件,然后按照这些步骤操作,并按照教程建议实施.

让我们假设您有控制器仪表板,并且方法addRoom显示您的html表单:

class Dashboard extends CI_Controller {


    function addRoom() {

        // Detect URI: http://example.com/dashboard/addRoom/upload_file
        $upload_req = $this->uri->segment("3");

        if( $upload_req == "upload_file" ) {
            // Do the file upload Actions
        }

        // Generate Form
        $this->load->view("Generate_view");
    }

}
Run Code Online (Sandbox Code Playgroud)

视图:

<?php 
    $ar = array("class"=>"form-horizontal");
    echo form_open_multipart('dashboard/addRoom/upload_file',$ar);
 ?>
 <input type="text" name="fileName">
 <input type="file" name="upload">
 <input type="submit" value="add">
 <?php echo form_close(); ?>
Run Code Online (Sandbox Code Playgroud)

在这种情况下,即使您提交或不提交,您的表单仍然在同一个控制器和方法上.

更新:根据更新的答案,请使用codeigniter默认库进行会话和文件上传,如果出现JS错误,请使用控制台选项卡(Chrome)调试错误,或者如果您使用的是firefox,则使用firebug扩展来调试Javascript.