自定义ELGG(1.8)插件将图片上传到实体

时间:2014-01-02 10:17:00

标签: php plugins elgg

这可能是一件非常简单的事情,但事实证明这对我来说很难实现。

我正在为elgg创建一个自定义插件来构建一个库。我希望能够在创建新项目时上传图像。

目前在我的观点/ default / form / cust_plugin / save.php中我有

elgg_view('input/file',array('name'=>'image','value'=>$image);

在action / cust_plugin / save.php中我有

$cust_plugin->image = $image;

但这不起作用。

我错过了什么,我做错了什么?

由于

1 个答案:

答案 0 :(得分:0)

要上传图片我使用“文件”插件,因此我可以非常简单地管理它。 在我的功能中,我打电话给

//Load file library
 elgg_load_library('elgg:file');
 //Set multipart/form-data attribute on my form
 $vars = array('enctype' => 'multipart/form-data');
 $body_vars = file_prepare_form_vars();

//Create Array values to return
$return = array(
    'title' => elgg_echo('gallery:title:add'),
    'content' => elgg_view_form('elgg-gallery/uppa', $vars,$body_vars) //My form
    );

我的表格是:

<div>
<label for="image_upload">Image upload</label>
<?php echo elgg_view('input/file', array('name' => 'img_upload')); ?>
</div>

我的行动:

if (empty($_FILES['img_upload']['name']))
{
    $error = elgg_echo('file:nofile');
    register_error($error);
    forward(REFERER);
 }

  //Make a file
  $file = new FilePluginFile();
  $file->subtype = "file";

  // if no title, grab filename
  if (empty($titolo))
     $titolo = htmlspecialchars($_FILES['img_upload']['name'], ENT_QUOTES, 'UTF-8');

   $file->title = $titolo;
   $file->description = "description file";
   $file->access_id = ACCESS_PUBLIC;
   $file->owner_guid = elgg_get_logged_in_user_guid();

   // we have a file upload, so process it
   if (isset($_FILES['img_upload']['name']) && !empty($_FILES['img_upload']['name']))
   {
     //Generate filename
     $prefix = "file/";
$filestorename = elgg_strtolower(time().$_FILES['img_upload']['name']);
$file->setFilename($prefix . $filestorename);
//Set Mimetype
$mime_type = ElggFile::detectMimeType($_FILES['img_upload']['tmp_name'], $_FILES['img_upload']['type']);
$file->setMimeType($mime_type);
//Set attributes
$file->originalfilename = $_FILES['img_upload']['name'];
$file->simpletype = file_get_simple_type($mime_type);
// Open the file to guarantee the directory exists
$file->open("write");
$file->close();
//Move file
move_uploaded_file($_FILES['img_upload']['tmp_name'], $file->getFilenameOnFilestore());
//Save file
$guid = $file->save();

//Make thumbnails
if ($guid && $file->simpletype == "image")
{
    $file->icontime = time();
    $thumbnail = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), 60, 60, true);
    if ($thumbnail)
    {
        $thumb = new ElggFile();
        $thumb->setMimeType($_FILES['img_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);
    }
}
if ($guid)
{
    $message = elgg_echo("gallery:status:upsuccess");
    system_message($message);
    forward($guid->getURL());
}