uploadProgress.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. /**
  3. * Simple Ajax Uploader
  4. * Version 2.6.2
  5. * https://github.com/LPology/Simple-Ajax-Uploader
  6. *
  7. * Copyright 2012-2017 LPology, LLC
  8. * Released under the MIT license
  9. *
  10. * Returns upload progress updates for browsers that don't support the HTML5 File API.
  11. * Falling back to this method allows for upload progress support across virtually all browsers.
  12. *
  13. */
  14. // This "if" statement is only necessary for CORS uploads -- if you're
  15. // only doing same-domain uploads then you can delete it if you want
  16. if (isset($_SERVER['HTTP_ORIGIN'])) {
  17. header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
  18. header('Access-Control-Allow-Credentials: true');
  19. header('Access-Control-Max-Age: 86400'); // cache for 1 day
  20. }
  21. if (isset($_REQUEST['progresskey'])) {
  22. $status = apc_fetch('upload_'.$_REQUEST['progresskey']);
  23. } else {
  24. exit(json_encode(array('success' => false)));
  25. }
  26. $pct = 0;
  27. $size = 0;
  28. if (is_array($status)) {
  29. if (array_key_exists('total', $status) && array_key_exists('current', $status)) {
  30. if ($status['total'] > 0) {
  31. $pct = round(($status['current'] / $status['total']) * 100);
  32. $size = round($status['total'] / 1024);
  33. }
  34. }
  35. }
  36. echo json_encode(array('success' => true, 'pct' => $pct, 'size' => $size));