I'm developing a custom component for creating, editing and saving data of single records including images. I know that in admin/models/forms/record.xml there must be field name="image" type="file" accept="image/*" but how and where do I need to write the simple php-code for uploading images to specific folder, in which file and how? Where to write the code from example by the link https://docs.joomla.org/How_to_use_the_filesystem_package ?

EDITED: Now I added in my admin/com_mycom/controllers/record.php the code:

public function save($data = array(), $key = 'id')
        $file = JRequest::getVar('image', null, 'files', 'array');
        $filename = JFile::makeSafe($file['name']);
        $src = $file['tmp_name'];
        $dest = "media/com_mycom/img/".$filename;
        if (JFile::upload($src, $dest)) {
              //Redirect to a page of your choice
        } else {
              //Redirect and throw an error message

$data = array(), $key = 'id' I got from function allowEdit()

SOLVED BUT... So the problem was in the name of input. JRequest::getVar contains 'image' but input type="file" has name="jform[image]". I've found the code:

$jinput = JFactory::getApplication()->input;
$files = $jinput->files->get('jform');
$file = $files['image'];

instead of

$file = JRequest::getVar('image', null, 'files', 'array');

and now the file is being uploaded but unfortunately its name is not being updated in the DB

Ok, I've added under if (JFile::upload($src, $dest)) { the code:

$thisID = JRequest::getVar('id');
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$fields = array($db->quoteName('image') . " = " . $db->quote($filename));
$conditions = array($db->quoteName('id') . " = " . $thisID);

but is there some simplest way to update filename in the DB ?


You should add it to your controller, probably controllers/record.php. Override save function.

public function save($key = NULL, $urlVar = NULL)
  // Upload your image

  // Save item


"Failed to move file" error

This came from wrong $dest value. Destination has to be full path.


$dest = "media/com_mycom/img/".$filename;


$dest = JPATH_SITE."/media/com_mycom/img/".$filename;
| improve this answer | |
  • Thanks. Now I get the Warning: Failed to move file! All the needed folders have 0777 – stckvrw Apr 14 '15 at 8:19
  • Please update your question and add save function to it. Is /tmp folder also include in "all the needed foldser"? Usually this error gomes if /tmp folder is not writable. Go to System > System Information > Directory Permissions and recheck if all directories are writable. – Rene Korss Apr 14 '15 at 8:25
  • Yes, tmp also has 0777 and all the directories on the Directory Permissions tab are writable – stckvrw Apr 14 '15 at 8:41
  • Does your form have attribute enctype="multipart/form-data"? If not, then files won't be uploaded. – Rene Korss Apr 14 '15 at 8:43
  • Yes, it does and name of image file is being saved successfully in the DB but file doesn't move to the folder – stckvrw Apr 14 '15 at 8:43

I would suggest you to refer administrator part of com_media component. It has all file upload related functionality developed.

It also includes error handling and other checks related to file system.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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