upload.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /**
  3. * Upload images action
  4. */
  5. elgg_load_library('elgg:gallery_field');
  6. $files_array = $_FILES['image'];
  7. $entity_id = $_POST['entity_id'];
  8. $entity_field = $_POST['entity_field'];
  9. $entity = get_entity($entity_id);
  10. if(false == $entity->canEdit())
  11. {
  12. register_error(elgg_echo('gallery_field:cant_edit'));
  13. forward(REFERRER);
  14. }
  15. $image_ids = gallery_field_image_ids_from_value($entity->$entity_field);
  16. $count_added = 0;
  17. for($i = 0; $i < count($files_array['tmp_name']); $i++)
  18. {
  19. if(false == in_array($files_array['type'][$i], array("image/jpeg","image/jpg")))
  20. {
  21. if(strlen($files_array['type'][$i]) == 0)
  22. {
  23. register_error(elgg_echo("gallery_field:max_upload_exceed"));
  24. }
  25. else
  26. {
  27. register_error(elgg_echo("gallery_field:only_jpg"));
  28. }
  29. continue;
  30. }
  31. $file = \GalleryFieldImage::createFromFile($files_array['tmp_name'][$i], $files_array['type'][$i]);
  32. if($file == null)
  33. {
  34. continue;
  35. }
  36. $file->entity_id = $entity_id;
  37. $file->save();
  38. array_unshift($image_ids, $file->guid);
  39. $count_added++;
  40. }
  41. $entity->$entity_field = implode(",", $image_ids);
  42. system_messages(elgg_echo("gallery_field:files_uploaded").": " . $count_added);
  43. forward($_SERVER['HTTP_REFERER']."#editor_".$entity_field);