0

I cant find out why Joomla is not allowing to upload images through .xml created form.

I have a field for file upload

<field name="nuotrauka" type="file"
label="COM_DALYVIAI_FORM_LBL_DALYVIS_NUOTRAUKA"
description="COM_DALYVIAI_FORM_DESC_DALYVIS_NUOTRAUKA" 
upload_directory="/images/"
accept="image/*" /> 

After form submit I get error: "Error: This filetype is not allowed"

I tried .jpg, .png filetypes.

user1876234
  • 857
  • 2
  • 14
  • 28
  • Hmm that's odd. If I add `image/*` then no files appear for me when browsing, only folders – Lodder Sep 01 '14 at 09:02
  • For me, all image files are shown, but uploading is restricted. weird. Both in backend and frontend. – user1876234 Sep 01 '14 at 09:06
  • Can you please show the PHP you're using to manage the file? It might be caused by this. If you have no PHP to manage it, then please ensure you do as the XML code you've provided will not do anything apart from display the form field – Lodder Sep 01 '14 at 09:08
  • I don't have PHP file to manipulate it. I thought it will first upload file to images folder, before i can manipulate it. – user1876234 Sep 01 '14 at 09:12
  • Where did you get upload_directory from? It is not documented anywhere. Additionally, have you made sure that you're using enctype="multipart/form-data" in your form? – itoctopus Sep 01 '14 at 13:49
  • Use type="media" not type="file" otherwise you have to extend file to implement the upload. File just gives you a list of files in the folder. – Elin Sep 01 '14 at 18:13

1 Answers1

0

The issue is the Joomla file field is nothing more but a means to generate the form fields to select a file. This means there is no built in logic in the core to handle file submissions server side when a form is submitted. You would need to add logic to your components controller to grab the info from the request and save it where needed. I pasted a sample method from a project's controller that required file uploads.

public function upload() {
    //JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
    $jinput = JFactory::getApplication()->input;
    jimport('joomla.filesystem.file');

    // We're putting all our files in a directory called images.
    $path = 'images/uploaded/';

    // The posted data, for reference
    $file = $jinput->get('value', '', 'string');
    $name = $jinput->get('name', '', 'string');

    // Get the mime
    $getMime = explode('.', $name);
    $mime = end($getMime);

    // Separate out the data
    $data = explode(',', $file);

    echo "<h1>" . $path . $name . "</h1>";

    // Encode it correctly
    $encodedData = str_replace(' ','+',$data[1]);
    $decodedData = base64_decode($encodedData);
    //if (JFile::upload($decodedData, $path . $name)) {
    if(file_put_contents($path . $name, $decodedData)) {
        JError::raiseNotice(null, $name . ": uploaded successfully");
    } else {
        // Show an error message should something go wrong.
        JError::raiseError(null, "Something went wrong. Check that the file isn't corrupted");
    }
}

If you've already written the server side file upload controller method and you've confirmed all the other suggestions are not the issue, you should include the code in your question as chances are that's where the error is occurring.

Brian Bolli
  • 1,873
  • 1
  • 12
  • 14