script.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * Swiff.Uploader Example Backend
  4. *
  5. * This file represents a simple logging, validation and output.
  6. * *
  7. * WARNING: If you really copy these lines in your backend without
  8. * any modification, there is something seriously wrong! Drop me a line
  9. * and I can give you a good rate for fancy and customised installation.
  10. *
  11. * No showcase represents 100% an actual real world file handling,
  12. * you need to move and process the file in your own code!
  13. * Just like you would do it with other uploaded files, nothing
  14. * special.
  15. *
  16. * @license MIT License
  17. *
  18. * @author Harald Kirschner <mail [at] digitarald [dot] de>
  19. * @copyright Authors
  20. *
  21. */
  22. /**
  23. * Only needed if you have a logged in user, see option appendCookieData,
  24. * which adds session id and other available cookies to the sent data.
  25. *
  26. * session_id($_POST['SID']); // whatever your session name is, adapt that!
  27. * session_start();
  28. */
  29. // Request log
  30. /**
  31. * You don't need to log, this is just for the showcase. Better remove
  32. * those lines for production since the log contains detailed file
  33. * information.
  34. */
  35. $result = array();
  36. $result['time'] = date('r');
  37. $result['addr'] = substr_replace(gethostbyaddr($_SERVER['REMOTE_ADDR']), '******', 0, 6);
  38. $result['agent'] = $_SERVER['HTTP_USER_AGENT'];
  39. if (count($_GET)) {
  40. $result['get'] = $_GET;
  41. }
  42. if (count($_POST)) {
  43. $result['post'] = $_POST;
  44. }
  45. if (count($_FILES)) {
  46. $result['files'] = $_FILES;
  47. }
  48. // we kill an old file to keep the size small
  49. if (file_exists('script.log') && filesize('script.log') > 102400) {
  50. unlink('script.log');
  51. }
  52. $log = @fopen('script.log', 'a');
  53. if ($log) {
  54. fputs($log, print_r($result, true) . "\n---\n");
  55. fclose($log);
  56. }
  57. // Validation
  58. $error = false;
  59. if (!isset($_FILES['Filedata']) || !is_uploaded_file($_FILES['Filedata']['tmp_name'])) {
  60. $error = 'Invalid Upload';
  61. }
  62. /**
  63. * You would add more validation, checking image type or user rights.
  64. *
  65. if (!$error && $_FILES['Filedata']['size'] > 2 * 1024 * 1024)
  66. {
  67. $error = 'Please upload only files smaller than 2Mb!';
  68. }
  69. if (!$error && !($size = @getimagesize($_FILES['Filedata']['tmp_name']) ) )
  70. {
  71. $error = 'Please upload only images, no other files are supported.';
  72. }
  73. if (!$error && !in_array($size[2], array(1, 2, 3, 7, 8) ) )
  74. {
  75. $error = 'Please upload only images of type JPEG, GIF or PNG.';
  76. }
  77. if (!$error && ($size[0] < 25) || ($size[1] < 25))
  78. {
  79. $error = 'Please upload an image bigger than 25px.';
  80. }
  81. */
  82. // Processing
  83. /**
  84. * Its a demo, you would move or process the file like:
  85. *
  86. * move_uploaded_file($_FILES['Filedata']['tmp_name'], '../uploads/' . $_FILES['Filedata']['name']);
  87. * $return['src'] = '/uploads/' . $_FILES['Filedata']['name'];
  88. *
  89. * or
  90. *
  91. * $return['link'] = YourImageLibrary::createThumbnail($_FILES['Filedata']['tmp_name']);
  92. *
  93. */
  94. if ($error) {
  95. $return = array(
  96. 'status' => '0',
  97. 'error' => $error
  98. );
  99. } else {
  100. $return = array(
  101. 'status' => '1',
  102. 'name' => $_FILES['Filedata']['name']
  103. );
  104. // Our processing, we get a hash value from the file
  105. $return['hash'] = md5_file($_FILES['Filedata']['tmp_name']);
  106. // ... and if available, we get image data
  107. $info = @getimagesize($_FILES['Filedata']['tmp_name']);
  108. if ($info) {
  109. $return['width'] = $info[0];
  110. $return['height'] = $info[1];
  111. $return['mime'] = $info['mime'];
  112. }
  113. }
  114. // Output
  115. /**
  116. * Again, a demo case. We can switch here, for different showcases
  117. * between different formats. You can also return plain data, like an URL
  118. * or whatever you want.
  119. *
  120. * The Content-type headers are uncommented, since Flash doesn't care for them
  121. * anyway. This way also the IFrame-based uploader sees the content.
  122. */
  123. if (isset($_REQUEST['response']) && $_REQUEST['response'] == 'xml') {
  124. // header('Content-type: text/xml');
  125. // Really dirty, use DOM and CDATA section!
  126. echo '<response>';
  127. foreach ($return as $key => $value) {
  128. echo "<$key><![CDATA[$value]]></$key>";
  129. }
  130. echo '</response>';
  131. } else {
  132. // header('Content-type: application/json');
  133. echo json_encode($return);
  134. }
  135. ?>