/** * Returns array('success'=>true) or array('error'=>'error message') */ function handleUpload($uploadDirectory, $replaceOldFile = FALSE) { elgg_load_library('elgg:shoutout'); if (!is_writable($uploadDirectory)) { return array('error' => "Server error. Upload directory isn't writable."); } if (!$this->file) { return array('error' => 'No files were uploaded.'); } $size = $this->file->getSize(); if ($size == 0) { return array('error' => 'File is empty'); } if ($size > $this->sizeLimit) { return array('error' => 'File is too large'); } $pathinfo = pathinfo($this->file->getName()); $filename = $pathinfo['filename']; //$filename = md5(uniqid()); $ext = $pathinfo['extension']; if ($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)) { $these = implode(', ', $this->allowedExtensions); return array('error' => 'File has an invalid extension, it should be one of ' . $these . '.'); } if (!$replaceOldFile) { /// don't overwrite previous files that were uploaded while (file_exists($uploadDirectory . $filename . '.' . $ext)) { $filename .= rand(10, 99); } } $full_file_path = $uploadDirectory . $filename . '.' . $ext; if ($this->file->save($full_file_path)) { $guid = elgg_get_logged_in_user_guid(); $upload_bits = explode('/', $uploadDirectory); $time_bit = $upload_bits[count($upload_bits) - 2]; if (in_array($ext, array('png', 'jpg', 'jpeg', 'gif'))) { $thumb = elgg_get_site_url() . 'shoutout/temporary_thumb/' . $guid . '/' . $time_bit . '/' . $filename . '.' . $ext; } else { $mime_type = shoutout_determine_mime_type($full_file_path); $thumb = shoutout_get_file_icon_url($mime_type); } $delete = 'action/shoutout/attach/delete?guid=' . $guid . '&time_bit=' . $time_bit . '&filename=' . $filename . '.' . $ext; return array('success' => true, 'uploadDirectory' => $time_bit, 'thumb' => $thumb, 'delete' => $delete); } else { return array('error' => 'Could not save uploaded file.' . 'The upload was cancelled, or server error encountered'); } }
function shoutout_download_attachment($annotation_id) { $annotation = elgg_get_annotation_from_id($annotation_id); if ($annotation) { $entity_guid = $annotation->entity_guid; if (get_entity($entity_guid)) { list($time_bit, $ofn) = explode('|', $annotation->value); $full_file_path = elgg_get_data_path() . shoutout_attach_make_file_matrix($annotation->owner_guid) . '/attachments/' . $time_bit . '/' . $ofn; // determine mime type $mime_type = shoutout_determine_mime_type($full_file_path); header("Content-Type: {$mime_type}"); header("Content-Disposition: attachment; filename=\"{$ofn}\""); $content = file_get_contents($full_file_path); $splitString = str_split($content, 8192); foreach ($splitString as $chunk) { echo $chunk; } } } exit; }