4

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
        }
        parent::save();
    }

$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);
$query->update($db->quoteName('#__mycom'))->set($fields)->where($conditions);
$db->setQuery($query);
$db->execute($query);

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

1

2 Answers 2

4

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
  parent::save();
}

UPDATE

"Failed to move file" error

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

Wrong:

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

Correct:

$dest = JPATH_SITE."/media/com_mycom/img/".$filename;
8
  • Thanks. Now I get the Warning: Failed to move file! All the needed folders have 0777
    – stckvrw
    Apr 14, 2015 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, 2015 at 8:25
  • Yes, tmp also has 0777 and all the directories on the Directory Permissions tab are writable
    – stckvrw
    Apr 14, 2015 at 8:41
  • Does your form have attribute enctype="multipart/form-data"? If not, then files won't be uploaded.
    – Rene Korss
    Apr 14, 2015 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, 2015 at 8:43
1

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.

Your Answer

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

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