php上传文件并显示上传进度的方法

本文实例讲述了php上传文件并显示上传进度的方法,分享给大家供大家参考,具体如下:

记得上传文件的时候要大点,不然还没看出来就上传完了,并且上传的文件不要太大,上G的就算了,2G的我试了,PHP受不了,我测试的是300多M的,记得要调整小php.ini参数啊 “选文件=》提交=》获取信息”要一气呵成哦^ ^

  1. <?php
  2. $prefix = ini_get('session.upload_progress.prefix');
  3. $name = ini_get('session.upload_progress.name');
  4. $key = $prefix . $name;
  5. session_start();
  6. if (isset($_POST['get_info'])) {
  7. $logo = $prefix . $_POST['logo'];
  8. exit(json_encode($_SESSION[$logo]));
  9. } elseif ($_POST) {
  10. echo '<script>var finashed = true;</script>';
  11. }
  12. ?>
  13. <div ></div>
  14. <form action="index.php" method="POST" enctype="multipart/form-data">
  15. <input type="hidden" name="<?php echo $name; ?>" value="test">
  16. <input type="file" name="file"><br>
  17. <input type="submit" value="提交">
  18. <input type="button" value="获取信息" onclick="getUploadInfo()">
  19. </form>
  20. <script src="jquery.js"></script>
  21. <script>
  22. var sto = null;
  23. var progress = null;
  24. function getUploadInfo()
  25. {
  26. $.post("index.php", {"get_info": 1, "logo": "test"}, function(data)
  27. {
  28. data = eval("(" + data + ")");
  29. progress = parseInt(parseInt(data.bytes_processed) * 10000 / parseInt(data.content_length)) / 100 + "%";
  30. document.getElementById("show_info_div").innerHTML = progress;
  31. sto = setTimeout("getUploadInfo()", 1000);
  32. });
  33. }
  34. if (typeof(finashed) !== "undefined")
  35. {
  36. document.getElementById("show_info_div").innerHTML = "100.00% (上传成功!)";
  37. }
  38. </script>