platform/plugins/Q/handlers/Q/file/post.php - Q Platform
Show:

File: platform/plugins/Q/handlers/Q/file/post.php

  1. <?php
  2.  
  3. /**
  4. * @module Q
  5. */
  6.  
  7. /**
  8. * Used by HTTP clients to upload a new file to the server
  9. * @class HTTP Q file
  10. * @method post
  11. * @param {array} [$params] Parameters that can come from the request
  12. * @param {string} [$params.data] Required if $_FILES is empty. Base64-encoded image data URI - see RFC 2397
  13. * @param {string} [$params.path="uploads"] parent path under web dir (see subpath)
  14. * @param {string} [$params.subpath=""] subpath that should follow the path, to save the image under
  15. * @param {string} [$params.name] override the name of the file, after the subpath
  16. * @param {boolean} [$params.audio] set this to true if the file is an audio file
  17. */
  18. function Q_file_post($params = null)
  19. {
  20. $p = $params
  21. ? $params
  22. : Q::take($_REQUEST, array('data', 'path', 'subpath', 'audio'));
  23. if (!empty($_FILES)) {
  24. $file = reset($_FILES);
  25. if ($tmp = $file['tmp_name']) {
  26. if (empty($p['data'])) {
  27. $p['data'] = file_get_contents($tmp);
  28. $p['name'] = $file['name'];
  29. }
  30. unlink($tmp);
  31. }
  32. } else {
  33. if (empty($p['data'])) {
  34. throw new Q_Exception_RequiredField(array('field' => 'data'), 'data');
  35. }
  36. $p['data'] = base64_decode(chunk_split(substr($p['data'], strpos($p['data'], ',')+1)));
  37. }
  38. $timeLimit = Q_Config::get('Q', 'uploads', 'limits', 'file', 'time', 5*60*60);
  39. set_time_limit($timeLimit); // default is 5 min
  40. $data = Q_File::save($p);
  41. if (empty($params)) {
  42. Q_Response::setSlot('data', $data);
  43. }
  44. return $data;
  45. }
  46.