/** * Saves entity media * * @param mixed $file Path to file, or an array with 'tmp_name' and 'name', or ElggFile * @param \ElggEntity $entity Entity * @param string $type Media type * @return \ElggFile */ function elgg_save_media($file, \ElggEntity $entity, $type = 'icon') { if ($file instanceof ElggFile) { $path = $file->getFilenameOnFilestore(); } else { if (is_array($file)) { $path = elgg_extract('tmp_name', (array) $file); } else { $path = (string) $file; } } if (!file_exists($path)) { return false; } elgg_remove_media($entity, $type); $filename = is_string($file) ? basename($path) : elgg_extract('name', (array) $file); $originalfilename = time() . $filename; // in case it's named 'large.jpg' or similar $filehandler = new ElggFile(); $filehandler->owner_guid = $entity->guid; $filehandler->setFilename("media/{$type}/{$originalfilename}"); $filehandler->open('write'); $filehandler->write(file_get_contents($path)); $filehandler->close(); if (!$filehandler->exists()) { return false; } $entity->{"{$type}_originalfilename"} = $originalfilename; return $filehandler; }
<?php $guid = get_input('guid'); $type = get_input('type'); $entity = get_entity($guid); if (!$entity || !$entity->canEdit() || !elgg_has_media($entity, $type)) { register_error(elgg_echo('media:remove:error:no_media')); forward(REFERER); } if (elgg_remove_media($entity, $type)) { system_message(elgg_echo('media:remove:succes')); } else { register_error(elgg_echo('media:remove:error')); } forward(REFERER);