1

I've created a custom component in which my client can add and manage different accommodations. I would like to automatically create a folder (with name of Alias) inside '/images' whenever the client adds a new object (accommodation) to the component. I did find that I should be able to fire for example JFolder::create on onContentAfterSave, found a example of somebody using it to do the same in com_articles with use of a plugin, but somehow I cannot get it to work inside my custom component.

(To be complete: the issue here is that the mediamanager, when used in a subform like I do, does not show the option to create a folder. Hence I would like to add it this way).

Anyone any thoughts on this?

// code I have in edit.php of component

function onContentAfterSave(){
    $foldername = $accommodation->alias;

    // First we set up parameters
    $searchpath = JUri::root() . '/images/accommodations' . $foldername;

    // Joomla doesn't autoload JFile and JFolder
    JLoader::register('JFile', JPATH_LIBRARIES . '/joomla/filesystem/file.php');
    JLoader::register('JFolder', JPATH_LIBRARIES . '/joomla/filesystem/folder.php');

    // Then we create the subfolders
    if (!JFolder::create($searchpath . "/gallery"))
    {

    };
    if (!JFolder::create($searchpath . "/slider"))
    {

    };
};
1
  • It would help if we saw some code.
    – TryHarder
    Commented Sep 25, 2017 at 12:02

1 Answer 1

1

I think you may be a little confused with JUri::root(). This method doesn't provide a path, but a URL.

So your current code:

$searchpath = JUri::root() . '/images/accommodations' . $foldername;

will output:

http://example.com/images/accommodationsXXXXX


You'll need to make us of JPATH_SITE. So replace the above code with this:

$searchpath = JPATH_SITE . '/images/accommodations' . $foldername;

Hope this helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.