1

I know how to upload file given from jform if a component is already installed. But how to upload a file from my admin/assets/ folder of my component when installing the component? In my script.php I try the following code:

jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.file');
...
public function install($parent)
{
   // creating a folder
   $path = JPATH_ADMINISTRATOR . DIRECTORY_SEPARATOR . "templates" . DIRECTORY_SEPARATOR . "isis" . DIRECTORY_SEPARATOR . "html" . DIRECTORY_SEPARATOR . "com_content" . DIRECTORY_SEPARATOR . "article";
   $mode = 0755;
   JFolder::create($path, $mode);

   // uploading a file
   $file = JPATH_COMPONENT_ADMINISTRATOR . DIRECTORY_SEPARATOR . "assets" . DIRECTORY_SEPARATOR . "edit.php";
   $filename = JFile::makeSafe($file['name']);
   $src = $file['tmp_name'];
   $dest = JPATH_ADMINISTRATOR . DIRECTORY_SEPARATOR . "templates" . DIRECTORY_SEPARATOR . "isis" . DIRECTORY_SEPARATOR . "html" . DIRECTORY_SEPARATOR . "com_content" . DIRECTORY_SEPARATOR . "article" . DIRECTORY_SEPARATOR . $filename;
   JFile::upload($src, $dest);
}

The folder is being created successfully but the file is not being uploading to the folder and I get the can't move error

2
  • Are you sure you're not trying to move a file instead? Is this a file that comes as part of your component zip file?
    – Lodder
    Commented May 28, 2015 at 11:37
  • Yes, it's the part of my component. But I thought uploading and moving a file are the same no matter where the file comes from
    – stckvrw
    Commented May 28, 2015 at 12:42

3 Answers 3

1

I think you have got muddle up here. When you install an extension, you already uploading a zip file. So once Joomla had processed this zip, the files are already on the server. You cannot "upload" a file from one directory to another. You need to "move" or "copy" it.

You the code you'll need to use will either be:

JFile::move($src, $dest);

or:

JFile::copy($src, $dest);

And as @Nagarjun already said, you need to put this code in the postflight function rather than the install function. Ig you use the install function, the $src file that you define may not yet exist ;)

Hope this helps

1

Do your file movement tasks in postflight function of your script.php file

function postflight($type, $parent) 
{
  // do it here
  JFile::move($src, $dest);
}

This function get called after copying files from your package, databases gets updated etc.

-2

Here you can find a brief example of Joomla installer script file,

2
  • I think you missing the actual question here. It's not so much which function to put the code in, but the actual code itself
    – Lodder
    Commented May 28, 2015 at 13:41
  • Shared those core link with intention that @stckvrw can get more idea about what functions are available in install script. Commented May 29, 2015 at 6:56

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.