function add_image_to_album($file, $folder_path)
{
    global $site;
    $folder_path = trim($folder_path);
    // only public folders
    if (strpos($folder_path, 'public/') === 0) {
        $folder_id = create_folder_from_path($folder_path);
        if (is_int($folder_id)) {
            // upload the file
            $upload_result = upload_to_folder($file, $folder_id);
            if (is_int($upload_result)) {
                global $site, $class_path;
                $folder = new Objekt(array('objekt_id' => $folder_id));
                $conf = new CONFIG($folder->all['ttyyp_params']);
                include_once $class_path . 'picture.inc.php';
                generate_images($site->absolute_path . $conf->get('path'), $conf->get('tn_size'), $conf->get('pic_size'));
            }
            return $upload_result;
        } else {
            // error message
            return $folder_id;
        }
    } else {
        return 'no_such_folder';
    }
}
 $supplement = 2;
 // unlikely to happen
 if ($document_folder_path === '') {
     $document_folder_path = $clean_path = rand(10000, 20000);
 }
 while (file_exists($site->absolute_path . 'public/documents/' . $document_folder_path)) {
     $document_folder_path = create_alias_from_string($clean_path . '-' . $supplement);
     $supplement++;
     // guard, also unlikely
     if ($supplement > 1000) {
         exit;
     }
 }
 $document_folder_path = 'public/documents/' . $document_folder_path;
 // create folder
 $folder_id = create_folder_from_path($document_folder_path);
 if (is_int($folder_id)) {
     $sql = $site->db->prepare("select obj_dokument.objekt_id, obj_dokument.fail, obj_dokument.sisu_blob, obj_dokument.size from obj_dokument left join objekt_objekt on obj_dokument.objekt_id = objekt_objekt.objekt_id where parent_id = ?", $document['objekt_id']);
     $documents_result = new SQL($sql);
     while ($file = $documents_result->fetch('ASSOC')) {
         $parts_sql = $site->db->prepare('SELECT content FROM document_parts WHERE objekt_id = ? ORDER BY id ASC', $file['objekt_id']);
         $parts_result = new SQL($sql);
         while ($part = $parts_result->fetch()) {
             $file['sisu_blob'] .= $part['content'];
         }
         $fpc_result = file_put_contents($site->absolute_path . '/' . $document_folder_path . '/' . $file['fail'], $file['sisu_blob']);
         if ($fpc_result === false) {
             echo '<font color=red>Error: could not create file: ' . $document_folder_path . '/' . $file['fail'] . ', check filesystem permissions.</font>';
             exit;
         }
         $sql = $site->db->prepare("delete from obj_dokument where objekt_id = ?", $file['objekt_id']);