// if previous file, delete it if ($new_file == false) { $filename = $file->getFilenameOnFilestore(); if (file_exists($filename)) { unlink($filename); } // use same filename on the disk - ensures thumbnails are overwritten $filestorename = $file->getFilename(); $filestorename = substr($filestorename, strlen($prefix)); } else { $filestorename = strtolower(time() . $_FILES['upload']['name']); } $file->setFilename($prefix . $filestorename); $file->setMimeType($_FILES['upload']['type']); $file->originalfilename = $_FILES['upload']['name']; $file->simpletype = get_general_file_type($_FILES['upload']['type']); $file->open("write"); $file->write(get_uploaded_file('upload')); $file->close(); $guid = $file->save(); // if image, we need to create thumbnails (this should be moved into a function) if ($guid && $file->simpletype == "image") { $thumbnail = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), 60, 60, true); if ($thumbnail) { $thumb = new ElggFile(); $thumb->setMimeType($_FILES['upload']['type']); $thumb->setFilename($prefix . "thumb" . $filestorename); $thumb->open("write"); $thumb->write($thumbnail); $thumb->close(); $file->thumbnail = $prefix . "thumb" . $filestorename;
function create_file($container_guid, $title, $desc, $access_id, $guid, $tags, $new_file) { // register_error("Creating file: " . $container_guid . ", vars: " . print_r(array($title, $desc, $access_id, $guid, $tags, $new_file), true)); if ($new_file) { // must have a file if a new file upload if (empty($_FILES['upload']['name'])) { // cache information in session $_SESSION['uploadtitle'] = $title; $_SESSION['uploaddesc'] = $desc; $_SESSION['uploadtags'] = $tags; $_SESSION['uploadaccessid'] = $access_id; register_error(elgg_echo('file:nofile') . "no file new"); forward($_SERVER['HTTP_REFERER']); } $file = new FilePluginFile(); $file->subtype = "file"; // if no title on new upload, grab filename if (empty($title)) { $title = $_FILES['upload']['name']; } } else { // load original file object $file = get_entity($guid); if (!$file) { register_error(elgg_echo('file:cannotload') . 'can"t load existing'); forward($_SERVER['HTTP_REFERER']); } // user must be able to edit file if (!$file->canEdit()) { register_error(elgg_echo('file:noaccess') . 'no access to existing'); forward($_SERVER['HTTP_REFERER']); } } $file->title = $title; $file->description = $desc; $file->access_id = $access_id; $file->container_guid = $container_guid; $tags = explode(",", $tags); $file->tags = $tags; // we have a file upload, so process it if (isset($_FILES['upload']['name']) && !empty($_FILES['upload']['name'])) { $prefix = "file/"; // if previous file, delete it if ($new_file == false) { $filename = $file->getFilenameOnFilestore(); if (file_exists($filename)) { unlink($filename); } // use same filename on the disk - ensures thumbnails are overwritten $filestorename = $file->getFilename(); $filestorename = elgg_substr($filestorename, elgg_strlen($prefix)); } else { $filestorename = elgg_strtolower(time() . $_FILES['upload']['name']); } $file->setFilename($prefix . $filestorename); $file->setMimeType($_FILES['upload']['type']); $file->originalfilename = $_FILES['upload']['name']; $file->simpletype = get_general_file_type($_FILES['upload']['type']); $file->open("write"); $file->write(get_uploaded_file('upload')); $file->close(); $guid = $file->save(); // if image, we need to create thumbnails (this should be moved into a function) if ($guid && $file->simpletype == "image") { $thumbnail = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), 60, 60, true); if ($thumbnail) { $thumb = new ElggFile(); $thumb->setMimeType($_FILES['upload']['type']); $thumb->setFilename($prefix . "thumb" . $filestorename); $thumb->open("write"); $thumb->write($thumbnail); $thumb->close(); $file->thumbnail = $prefix . "thumb" . $filestorename; unset($thumbnail); } $thumbsmall = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), 153, 153, true); if ($thumbsmall) { $thumb->setFilename($prefix . "smallthumb" . $filestorename); $thumb->open("write"); $thumb->write($thumbsmall); $thumb->close(); $file->smallthumb = $prefix . "smallthumb" . $filestorename; unset($thumbsmall); } $thumblarge = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), 600, 600, false); if ($thumblarge) { $thumb->setFilename($prefix . "largethumb" . $filestorename); $thumb->open("write"); $thumb->write($thumblarge); $thumb->close(); $file->largethumb = $prefix . "largethumb" . $filestorename; unset($thumblarge); } } } else { // not saving a file but still need to save the entity to push attributes to database $file->save(); } return array($file, $guid); }
function form_handle_file_upload($fieldname, $access_id, $container_guid = 0) { // Extract file from, save to default filestore (for now) $prefix = "file/"; $file = new FilePluginFile(); $filestorename = strtolower(time() . $_FILES[$fieldname]['name']); $file->setFilename($prefix . $filestorename); $file->setMimeType($_FILES[$fieldname]['type']); $file->originalfilename = $_FILES[$fieldname]['name']; $file->subtype = "file"; $file->access_id = $access_id; $file->open("write"); $file->write(get_uploaded_file($fieldname)); $file->close(); if ($container_guid) { $file->container_guid = $container_guid; } $file->simpletype = get_general_file_type($_FILES[$fieldname]['type']); $result = $file->save(); if ($result) { form_generate_thumbnail($file, $fieldname); } return $file->getGUID(); }