/publish/application/helpers/imgupload_helper.php

https://github.com/mwq27/turnerphotography · PHP · 125 lines · 67 code · 25 blank · 33 comment · 18 complexity · 0208fe332aff5af4d4c995e0edfb7e8c MD5 · raw file

  1. <?php
  2. function custom_upload($type, $id){
  3. $ci =& get_instance();
  4. $ci->load->model('image');
  5. $targetDir = $_SERVER['DOCUMENT_ROOT'].'/images/';
  6. //$cleanupTargetDir = false; // Remove old files
  7. //$maxFileAge = 60 * 60; // Temp file age in seconds
  8. // 5 minutes execution time
  9. @set_time_limit(5 * 60);
  10. // Uncomment this one to fake upload time
  11. // usleep(5000);
  12. // Get parameters
  13. $chunk = isset($_REQUEST["chunk"]) ? $_REQUEST["chunk"] : 0;
  14. $chunks = isset($_REQUEST["chunks"]) ? $_REQUEST["chunks"] : 0;
  15. $fileName = isset($_REQUEST["name"]) ? $_REQUEST["name"] : '';
  16. // Clean the fileName for security reasons
  17. $fileName = preg_replace('/[^\w\._]+/', '', $fileName);
  18. // Make sure the fileName is unique but only if chunking is disabled
  19. if ($chunks < 2 && file_exists($targetDir . DIRECTORY_SEPARATOR . $fileName)) {
  20. $ext = strrpos($fileName, '.');
  21. $fileName_a = substr($fileName, 0, $ext);
  22. $fileName_b = substr($fileName, $ext);
  23. $count = 1;
  24. while (file_exists($targetDir . DIRECTORY_SEPARATOR . $fileName_a . '_' . $count . $fileName_b))
  25. $count++;
  26. $fileName = $fileName_a . '_' . $count . $fileName_b;
  27. }
  28. // Create target dir
  29. if (!file_exists($targetDir))
  30. @mkdir($targetDir);
  31. // Remove old temp files
  32. /* this doesn't really work by now
  33. if (is_dir($targetDir) && ($dir = opendir($targetDir))) {
  34. while (($file = readdir($dir)) !== false) {
  35. $filePath = $targetDir . DIRECTORY_SEPARATOR . $file;
  36. // Remove temp files if they are older than the max age
  37. if (preg_match('/\\.tmp$/', $file) && (filemtime($filePath) < time() - $maxFileAge))
  38. @unlink($filePath);
  39. }
  40. closedir($dir);
  41. } else
  42. die('{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "Failed to open temp directory."}, "id" : "id"}');
  43. */
  44. //INSERT INTO IMAGES TABLE
  45. switch($type){
  46. case "client":
  47. $ci->image->new_client_image($fileName, $id);
  48. break;
  49. case "category":
  50. $ci->image->new_image($fileName, $id);
  51. break;
  52. }
  53. // Look for the content type header
  54. if (isset($_SERVER["HTTP_CONTENT_TYPE"]))
  55. $contentType = $_SERVER["HTTP_CONTENT_TYPE"];
  56. if (isset($_SERVER["CONTENT_TYPE"]))
  57. $contentType = $_SERVER["CONTENT_TYPE"];
  58. // Handle non multipart uploads older WebKit versions didn't support multipart in HTML5
  59. if (strpos($contentType, "multipart") !== false) {
  60. if (isset($_FILES['file']['tmp_name']) && is_uploaded_file($_FILES['file']['tmp_name'])) {
  61. // Open temp file
  62. $out = fopen($targetDir . DIRECTORY_SEPARATOR . $fileName, $chunk == 0 ? "wb" : "ab");
  63. if ($out) {
  64. // Read binary input stream and append it to temp file
  65. $in = fopen($_FILES['file']['tmp_name'], "rb");
  66. if ($in) {
  67. while ($buff = fread($in, 4096))
  68. fwrite($out, $buff);
  69. } else
  70. die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
  71. fclose($in);
  72. fclose($out);
  73. @unlink($_FILES['file']['tmp_name']);
  74. } else
  75. die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
  76. } else
  77. die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}');
  78. } else {
  79. // Open temp file
  80. $out = fopen($targetDir . DIRECTORY_SEPARATOR . $fileName, $chunk == 0 ? "wb" : "ab");
  81. if ($out) {
  82. // Read binary input stream and append it to temp file
  83. $in = fopen("php://input", "rb");
  84. if ($in) {
  85. while ($buff = fread($in, 4096))
  86. fwrite($out, $buff);
  87. } else
  88. die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
  89. fclose($in);
  90. fclose($out);
  91. } else
  92. die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
  93. }
  94. // Return JSON-RPC response
  95. die('{"jsonrpc" : "2.0", "result" : null, "id" : "id"}');
  96. }
  97. ?>