【问题标题】:Create new folder in 'images' with every new article每篇新文章都在“图像”中创建新文件夹
【发布时间】:2016-07-08 04:29:59
【问题描述】:

我需要扩展标准的 Joomla 文章选项。 我想在“images/news/”文件夹中为文章图片创建新文件夹。每个新文件夹的名称将取自文章的别名。 我有 Javascript 方面的知识,但不知道如何使用 PHP 和我需要为此编辑的文件来做到这一点。我的网站在 Joomla 2.5 上运行,此时我无法更新它。所以请记住这一刻。

【问题讨论】:

    标签: php joomla2.5


    【解决方案1】:

    我对 Joomla 一无所知。但这是创建文件夹的 php 命令:

     $dirPath = 'images/news/'.$alias;
     $result = mkdir($dirPath);
    

    好吧,就像我说的,我真的不知道 joomla。但看起来并不难: https://docs.joomla.org/J2.5:Creating_a_content_plugin 所以我会试着给你一个开始。但是,如果有熟悉 Joomla 的人给出答案,我会听他们的 =)

    基本上,您的内容插件需要 2 个文件。一个 php 文件“myContentPlugin.php”,如下所示:

    <?php
    
     defined('_JEXEC') or die();
    
    jimport('joomla.plugin.plugin');
    
    
    class plgMyComponent extends JPlugin
    {
     public function __construct($subject, $config)
     { parent::__construct($subject, $config);
     }
    
     function onContentBeforeSave($context, &$article, $isNew)
     { global $mainframe;
    
    
     $dirPath = 'images/news/'.$articel->alias;
     $result = mkdir($dirPath);
    
     return true;
     }
    }
    

    还有一个如下所示的 XML 文件“myContentPlugin.xml”:

     <?xml version="1.0" encoding="utf-8"?>
      <extension version="1.7" type="plugin" group="content">
        <name>myContentPlugin</name>
        <author>JD</author>
        <creationDate>September 2011</creationDate>
        <copyright>Copyright (C) 2011 JD. All rights reserved.</copyright>
        <license>http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL</license>
        <authorEmail>me@mail.de</authorEmail>
        <authorUrl>www.domain.de</authorUrl>
        <version>1.7.0</version>
        <description></description>
        <files>
                <filename plugin="myContentPlugin">myContentPlugin.php</filename>
        </files>
    

    从这里找到http://forum.joomla.org/viewtopic.php?p=2607024

    【讨论】:

    • 感谢您尝试帮助我! Ofcource 我在 PHP 文档中找到了 mkdir 函数,但是当我保存我的文章时我不知道如何应用它。
    • 你不知道什么?什么时候触发它? onContentAfterSave 可以解决问题。
    • 是的。我不知道我需要在哪里创建文件夹。
    • 我认为您将需要 onContentBeforeSave 或 onContentAfterSave!看看这个:docs.joomla.org/Plugin/Events/Content
    • 就像我说的,不幸的是我是 PHP 新手,无法使用 ContentAfterSave 选项编写插件。但无论如何谢谢你:)
    【解决方案2】:

    所以,这里是完整的解决方案:

    将自定义字段类型复选框添加到文章的后端(您可以在内网上找到很多手册,所以我会简要介绍),例如“create_folder”;

    <fieldset name="image_folder" label="Image folder">
        <field name="create_folder"
            type="checkbox"
            label="Create automatically"
            value="1"
            default="0"
            filter="intval"
        />
    </fieldset>
    

    使用插件的 .xml 和 .php 文件创建插件文件夹(更多信息:https://docs.joomla.org/J2.5:Creating_a_Plugin_for_Joomla

    这是我的 .php 文件的代码:

    <?php
    
    defined('_JEXEC') or die;
    
    class plgContentImgDir extends JPlugin
    {
            public function onContentAfterSave($context,&$article){ 
            if($context=='com_content.article'){     
                    $folder_name=substr($article->alias,0,30);
                    $destination=JPATH_SITE.'/'."images/news";
                    if(!file_exists($destination.$folder_name)){       
                        JFolder::create($destination.'/'.$folder_name,0755);
                    }
            }
        }
    }
    
    ?>
    

    安装插件后,您将在文章设置中获得额外选项。如果复选框被选中,新文件夹将在 'images' 目录中创建。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-23
      • 1970-01-01
      相关资源
      最近更新 更多