Ruchika
Ruchika

Reputation: 515

Trying to Upload Image through Joomla Administrator Component Field, but not happening

I am trying to upload image through Joomla Administrator Component

In administrator\components\com_toys\models\forms\variant.xml i have defined

<?xml version="1.0" encoding="utf-8"?>
<form enctype="multipart/form-data">
<fieldset>
<field name="id" type="text" readonly="true" class="readonly" default="0" description="JGLOBAL_FIELD_ID_DESC"/>
<field name="v_prod_id" type="sql" label="Select Brand"/>   
<field name="v_name" type="text" label="Select Model"/>
<field name="v_descr" type="editor" buttons="true" filter="safehtml" label="Toy Details"/>
<field name="v_price" type="text" label="Price"/>   
<field name="v_small_img" type="file" label="Select Image"/>

<field name="state" type="list" label="JSTATUS" description="JFIELD_PUBLISHED_DESC" class="inputbox" size="1" default="0">
  <option value="1">JPUBLISHED</option>
  <option value="0">JUNPUBLISHED</option>
  <option value="2">JARCHIVED</option>
  <option value="-2">JTRASHED</option>
</field>

In the controller administrator\components\com_toys\controllers\variant.php im trying to retrieve that file like below

But the file is neither uploaded and nor saved in directory - /images/uploads/variants/ and also not saved in database

Can someone help pls what is wrong, why is the file not getting saved

Update It was easy as i was not aware that Joomla has inbuilt solution by declaring field type " />

Upvotes: 1

Views: 67

Answers (2)

Ruchika
Ruchika

Reputation: 515

Update It was easy as i was not aware that Joomla has inbuilt solution by declaring field type

<field name="v_small_img" type="media" directory="<upload path>" />

By choosing that image can be easily loaded in the desired directory In meanwhile - thanks Amit ray for taking an attempt

Upvotes: 0

Amit Ray
Amit Ray

Reputation: 3485

You are using wrong name of form

$files = $jinput->files->get('jform');

it is not jform it is v_small_img

So use this

    $input = new JInput;

    $file = $input->get('v_small_img', '', 'post'); 

Also dont use $_FILES again when you have $file. Use this instead

$file['tmp_name']; JFile::makeSafe($file['name']);

Wherever you have used $_FILES.

UPDATED with full code changes

public function save($key = null, $urlVar = null)
{
    $input = new JInput;

    $file = $input->get('v_small_img', '', 'post');
    //$file = $files['image'];

    $db = JFactory::getDBO();
    //$post   = JRequest::get( 'post' );
    $FILE_PATH= JPATH_BASE ."/images/uploads/variants/";
    //$StrFiles="";

    if((file_exists($file['tmp_name']))){
        if($file['name']!=""){
            $dt = time()."s";
            if(!JFile::upload($file['tmp_name'],$FILE_PATH.JFile::makeSafe($dt."_".$file['name']))){
                return JError::raiseWarning( 500, "Unable To upload File ".$file['name']);
            }else{
               // $StrFiles .=JFile::makeSafe($dt."_".$file['name']);
            }
        } else {
        return JError::raiseWarning( 500, "File name cannot be empty.");
    }
    }
    /*if($StrFiles){
        $post['v_small_img']= $StrFiles;
    }*/
}

Upvotes: 1

Related Questions