Not getting file upload to work

Hi folks,

started to work with elgg currently and trying to  make a plugin that handles it's own "types" (elggObject + metadata). I managed to add all the fields I needed, but I can't make elgg upload iamges/icons/w.e. thei're called.

$form = elgg_view("input/file", array("name" => "icon"));

echo elgg_view('input/form', array(
            'action'     => 'action/text_type/save',
            'enctype'     => 'multipart/form-data',
            'body'         => $form));

Is working, except in HTML the enctype is missing and (I believe) therefore uploading images isn't working ($_FILES on action page and get_uploaded_file('icon') both empty). What am I doing wrong?

  • That should be working. Try using elgg_view_form(), and if it works, report the issue on github.

  • Thanks. While applying your suggestion I found the error I made, but I have a problem getting/displaying these images now:

    $has_uploaded_icon = (!empty($_FILES['icon']['type']) && substr_count($_FILES['icon']['type'], 'image/'));
    
        if ($has_uploaded_icon) {
    
            $icon_sizes = elgg_get_config('icon_sizes');
    
            $prefix = "test/" . $test_guid;
    
            $filehandler = new ElggFile();
            $filehandler->owner_guid = $test->owner_guid;
            $filehandler->setFilename($prefix . ".jpg");
            $filehandler->open("write");
            $filehandler->write(get_uploaded_file('icon'));
            $filehandler->close();
            $filename = $filehandler->getFilenameOnFilestore();
    
            $sizes = array('tiny', 'small', 'medium', 'large', 'master');
    
            $thumbs = array();
            foreach ($sizes as $size) {
                $thumbs[$size] = get_resized_image_from_existing_file(
                    $filename,
                    $icon_sizes[$size]['w'],
                    $icon_sizes[$size]['h'],
                    $icon_sizes[$size]['square']
                    );
            }
    
            if ($thumbs['tiny']) { // just checking if resize successful
                $thumb = new ElggFile();
                $thumb->owner_guid = $test->owner_guid;
                $thumb->setMimeType('image/jpeg');
    
                foreach ($sizes as $size) {
                    $thumb->setFilename("{$prefix}{$size}.jpg");
                    $thumb->open("write");
                    $thumb->write($thumbs[$size]);
                    $thumb->close();
                }
    
                $test->icontime = time();
            }
        }

    to save the image and this in view to display it:

    echo elgg_view_entity_icon($vars['entity'], 'master');

    results in this error (loosely translated from german to english):

    The graphic "http://localhost/sites/elgg/mod/hypeFilestore/servers/icon.php?guid=109&dir_guid=36&path=icons%2F109master.jpg&ts=1491921441&size=master&mac=2ed882adc7599dbd268380cb3a9fcb14fd68141bc8c452e4ee88f92dd9a5a92a" can't be displayed, because it has errors.

    (obviously the path is wrong with "icons/" in it, but changing the save function to prefix with "icons" instead of "test" results in same error.

  • All the methods starting with set (e.g. setIconFromElggFile() ) are undefined.

    Got it to work somehow with:

    $owner = get_entity($test->owner_guid);
    
    if (!$owner || !($owner instanceof ElggUser) || !$owner->canEdit()) {
         register_error(elgg_echo('icon:upload:fail'));
         forward(REFERER);
    }
    
    $error = elgg_get_friendly_upload_error($_FILES['icon']['error']);
    if ($error) {     
        register_error($error);
         forward(REFERER);
    }
    
    if (!$test->saveIconFromUploadedFile('icon')) {
         register_error(elgg_echo('icon:resize:fail'));
         forward(REFERER);
    }

    But I want to store the images in their own directory instead of .../icons/...

  • http://learn.elgg.org/en/stable/guides/hooks-list.html
     
    entity:icon:file, <entity_type>
    Triggered by ElggEntity::getIcon() and allows plugins to provide an alternative ElggIcon object that points to a custom location of the icon on filestore. The handler must return an instance of ElggIcon or an exception will be thrown.
Beginning Developers

Beginning Developers

This space is for newcomers, who wish to build a new plugin or to customize an existing one to their liking