What API calls to add upload option?

Have a programming question regarding your component, plug-in, extension or core hacks? Have an interesting tidbit, FAQ or programming tip you’d like to share? This is the place for you.

Moderators: tjay, seadap, Rogue4ngel, matthewhayashida

Post Reply
User avatar
Skruu
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Thu Dec 07, 2006 8:04 pm

What API calls to add upload option?

Post by Skruu » Tue Sep 25, 2007 2:57 pm

Can anyone point me in the right direction concerning the following:

I would like to add an upload option to my component in the backend to upload a picture. I would like to do it in a good 1.5 way.
Does anyone have a piece of example code with the right API calls, so I can study it and take the code as a base?
Last edited by Skruu on Tue Sep 25, 2007 5:33 pm, edited 1 time in total.

User avatar
ianmac
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 237
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

Re: What API calls to add upload option?

Post by ianmac » Tue Sep 25, 2007 6:26 pm

I would take a look at the media manager in the backend.

Ian
Help test my Component XML Generator Tool!
http://extensions.joomla.org/component/option,com_mtree/task,viewlink/link_id,1997/Itemid,35/
All feedback appreciated!

zian974
Joomla! Apprentice
Joomla! Apprentice
Posts: 7
Joined: Mon Sep 10, 2007 9:41 am

Re: What API calls to add upload option?

Post by zian974 » Tue Oct 02, 2007 11:30 am

Hi,

I made this to upload txt file for my component. this is the function i wrote, free inspirate by './administrator/components/com_installer/models/install.php'. I think it work for other type of file (png, jpg, ...).

To manipulate File and folder, you need to import JFile and JFolder class.
I use JRequest::getVar with the attribute 'files'.

Code: Select all

public function upload(  )
{
   jimport('joomla.filesystem.*');
      
   // Récupération du fichier
   $userfile = JRequest::getVar('fileEsp', null, 'files', 'array' );
      
   // Check if there was a problem uploading the file.
   if ( $userfile['error'] || $userfile['size'] < 1 )
   {
      JError::raiseWarning('SOME_ERROR_CODE',  'Erreur de transfert du fichier' );
      return false;
   }

   // Build the appropriate paths
   $tmp_dest = PATH_TXTUPL.DS.'especes.txt';
   $tmp_src   = $userfile['tmp_name'];
      
   // Write the file $tmp_src in $tmp_dest folder
   if ( JFile::upload($tmp_src, $tmp_dest ) )
   {
      return true;
   }
      else
      {
         return false;   
      }
  }


And the form

           enctype="multipart/form-data" method="post"
           action="">

         

A partir du fichier .txt



                   
         


         



I hope this will help you.

Na retrouvé.
Last edited by zian974 on Tue Oct 02, 2007 11:32 am, edited 1 time in total.

ghostrifle
Joomla! Apprentice
Joomla! Apprentice
Posts: 16
Joined: Wed Aug 22, 2007 8:41 am
Location: Germany
Contact:

Re: What API calls to add upload option?

Post by ghostrifle » Tue Oct 02, 2007 10:30 pm

Well... I tried your example code but I doesn't work 'cause the task doesn't get called.

I implemented the upload-function in my controller where I edit, add and display all other single entries of my component.

Changing the input string of JRoute to match with my component doesn't work also:

Code: Select all

<form   class="formEsp" name="upL_txtEsp" id="upL_txtEsp" enctype="multipart/form-data" method="post"action="<?php echo JRoute::_( 'index.php?option=com_redlight&controller=redlight&task=upload&id='.$this->rl_ad->id ) ?>">
            <fieldset>
         <legend>Anzeigenfoto hochladen:</legend>  
         <p>
            <label for="fileEsp" title="Wähle ein Foto aus">Foto</label>
                <input type="file" name="fileEsp" id="fileEsp" title="Wähle ein Foto aus" size="50"/>
            <input type="submit" value="hinzufügen" size="50"/>
         </p>  
          </fieldset>  
      </form>


Well... here'ss my editied code from the controller. If the method would get called, I would either get a "noooo" or "yeeeees" message:

Code: Select all

function upload()
   {
      jimport('joomla.filesystem.*');
      
      // Récupération du fichier
      $userfile = JRequest::getVar('fileEsp', null, 'files', 'array' );
      
      // Check if there was a problem uploading the file.
      if ( $userfile['error'] || $userfile['size'] < 1 )
      {
         JError::raiseWarning('SOME_ERROR_CODE',  'noooooo' );
         return false;
      }
      else JError::raiseWarning('SOME_ERROR_CODE',  'yeeeees' );

      // Build the appropriate paths
      $tmp_dest = PATH_TXTUPL.DS.'especes.txt';
      $tmp_src   = $userfile['tmp_name'];
      
      // Write the file $tmp_src in $tmp_dest folder
      if ( JFile::upload($tmp_src, $tmp_dest ) )
      {
         return true;
      }
      else
      {
         return false;   
      }
   }


but nothing happens

I also registered the task in the constructor of my controller:

Code: Select all

   function __construct()
   {
       parent::__construct();

       // Register Extra tasks
      // wir mappen add zu edit... edit ist ja nix anders wie add nur mit gefüllter form etc..
       $this->registerTask( 'add'  ,     'edit' );
      $this->registerTask( 'upload'  ,     'upload' );
   }
o

User avatar
ianmac
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 237
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

Re: What API calls to add upload option?

Post by ianmac » Wed Oct 03, 2007 6:16 am

check where you're getting your task from?  And check to make sure it is being retrieved properly.  You are sending it via get, and it might be looking for it via post.

Ian
Help test my Component XML Generator Tool!
http://extensions.joomla.org/component/option,com_mtree/task,viewlink/link_id,1997/Itemid,35/
All feedback appreciated!

ghostrifle
Joomla! Apprentice
Joomla! Apprentice
Posts: 16
Joined: Wed Aug 22, 2007 8:41 am
Location: Germany
Contact:

Re: What API calls to add upload option?

Post by ghostrifle » Wed Oct 03, 2007 7:40 am

Uhh... I'm using POST to send the file....  so.... I have to differ between GET and POST while retrieving a task?... that's the thirst thing I read now considering joomla tasks.... where can I get further information about this topic and the GET and POST differences in joomla tasks !?

Thanx, Alex
o

ghostrifle
Joomla! Apprentice
Joomla! Apprentice
Posts: 16
Joined: Wed Aug 22, 2007 8:41 am
Location: Germany
Contact:

Re: What API calls to add upload option?

Post by ghostrifle » Wed Oct 03, 2007 9:59 pm

Well... I found the error... I nested two forms ... it works now, thanx for your help !
o


Post Reply