I am working on a custom component, where at the backend interface - we want the admin user to be able to upload image and let that image directly save to a specified folder - /images/uploads/variants/ and also to save file name say - 1327171407s_Rubytoys.jpg in the database in column field - v_small_img
What we have done as of now is On path directory /administrator/component/models/form/variant.xml file the field is set along with other fields in variant.xml file
<field name="v_name" type="text" label="Variant" description="COM_NEWTOY_FORM_DESC_VARIANT_V_NAME" hint="COM_NEWTOY_FORM_LBL_VARIANT_V_NAME"/> <field name="v_descr" type="editor" filter="safehtml" label="COM_NEWTOY_FORM_LBL_VARIANT_V_DESCR" description="COM_NEWTOY_FORM_DESC_VARIANT_V_DESCR" hint="COM_NEWTOY_FORM_LBL_VARIANT_V_DESCR"/> <field name="v_price" type="text" label="Price" description="COM_NEWTOY_FORM_DESC_VARIANT_V_PRICE" hint="COM_NEWTOY_FORM_LBL_VARIANT_V_PRICE"/> <field name="v_small_img" type="file" label="COM_NEWTOY_FORM_LBL_VARIANT_V_SMALL_IMG" description="COM_NEWTOY_FORM_DESC_VARIANT_V_SMALL_IMG" hint="COM_NEWCAR_FORM_LBL_VARIANT_V_SMALL_IMG"/> The field v_small_img is showing an upload button. But I don't know:
How to set path of upload file to save in - public_html/images/uploads/variants/
How to save file name only in db - v_small_img (only file name to be saved, not the file as it has to be uploaded to separate directory - /images/uploads/variants/)
I have tried modifying save function in controller/variants.php but it's not helping either.
public function saveOrderAjax() { // Get the input $input = JFactory::getApplication()->input; $pks = $input->post->get('cid', array(), 'array'); $order = $input->post->get('order', array(), 'array'); $files = $input->files->get('jform'); $file = $files['image']; $filename = JFile::makeSafe($file['name']); $src = $file['tmp_name']; $dest = JPATH_SITE."images/uploads/variants/".$filename; if (JFile::upload($src, $dest)) { $thisID = JRequest::getVar('id'); $db = JFactory::getDbo(); $query = $db->getQuery(true); $v_small_img = array($db->quoteName('image') . " = " . $db->quote($filename)); $conditions = array($db->quoteName('id') . " = " . $thisID); $query->update($db->quoteName('#__NEWTOY_variants'))->set($v_small_img)->where($conditions); $db->setQuery($query); $db->execute($query); } else { //Redirect and throw an error message } // Sanitize the input ArrayHelper::toInteger($pks); ArrayHelper::toInteger($order); // Get the model $model = $this->getModel(); // Save the ordering $return = $model->saveorder($pks, $order); if ($return) { echo "1"; } // Close the application JFactory::getApplication()->close(); }