简体   繁体   中英

Create new folder in 'images' with every new article

I need to extend standard Joomla article options. I want to create new folder for article's images in 'images/news/' folder. The name for every new folder will be taken from article's alias. I'm have knowledge in Javascript, but have no idea how can I do this with PHP and wich files I need edit for this. My website works on Joomla 2.5 and at this moment I can't update it. So please keep in mind this moment.

I have no knowledge in Joomla. But this is the php command for creating a Folder:

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

Ok like I said, i don't really know joomla. But it doesn't look that hard: https://docs.joomla.org/J2.5:Creating_a_content_plugin So i will try to give you a start. But if there is an answer from somebody who is skilled in Joomla i would listen to them =)

Basically you Need 2 Files for your Content plugin. A php File "myContentPlugin.php" which will look like this:

<?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;
 }
}

and an XML file "myContentPlugin.xml" which will look like this:

 <?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>

As found from here http://forum.joomla.org/viewtopic.php?p=2607024

So, here is complete solution:

Add custom field type checkbox to article's backend (you can find many manuals over the inernet, so I'll be brief), for example '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>

Create plugin folder with .xml and .php files of your plugin (more info here: https://docs.joomla.org/J2.5:Creating_a_Plugin_for_Joomla )

This is code of my .php file:

<?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);
                }
        }
    }
}

?>

After you install your plugin, you'll get additional option in your article settings. If checkbox checked the new folder will be create in 'images' directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM