Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Save Uploaded File's Name on Database

Cont. - Add File Uploader to Joomla Admin Component

I could able to upload file and save it on disk. But its not saving file name on the database.

How can i do it ?

Here is the controller -

class InvoiceManagerControllerInvoiceManager extends JControllerForm
{
    function save(){
        $file = JRequest::getVar('jform', null, 'files', 'array');
        $path = JPATH_BASE;

        // Make the file name safe.
        jimport('joomla.filesystem.file');
        $file['name']['invoice'] = JFile::makeSafe($file['name']['invoice']);

        // Move the uploaded file into a permanent location.
        if (isset($file['name']['invoice'])) {
            // Make sure that the full file path is safe.
            $filepath = JPath::clean($path. DS ."components". DS ."com_invoicemanager". DS ."files". DS .strtolower($file['name']['invoice']));
            // Move the uploaded file.
            JFile::upload( $file['tmp_name']['invoice'], $filepath );
        }

        return parent::save();
    }
}

Form field in XML -

<field name="invoice" type="file"/>

UPDATE: worked after adding following lines taken from @Andras Gera code

$data = JRequest::getVar( 'jform', null, 'post', 'array' );
$data['invoice'] = strtolower( $file['name']['invoice'] );

JRequest::setVar('jform', $data );
like image 655
ChamingaD Avatar asked Dec 27 '22 16:12

ChamingaD


1 Answers

I've ran into the same problem, maybe we can go forward together. Here is my codes:

/administrator/components/com_comp_name/models/forms/edit.xml

<?xml version="1.0" encoding="utf-8"?>
<form addrulepath="/administrator/components/com_gonewsletter/models/rules">
    <fieldset name="details">
        <field
            name="id"
            type="hidden"
        />
        <field
            name="title"
            type="text"
            label="COM_GONEWSLETTER_EDIT_TITLE_LABEL"
            description="COM_GONEWSLETTER_EDIT_TITLE_DESC"
            size="40"
            class="inputbox"
            required="true"
            default=""
        />
        <field
            name="date"
            type="calendar"
            label="COM_GONEWSLETTER_EDIT_DATE_LABEL"
            description="COM_GONEWSLETTER_EDIT_DATE_DESC"
            size="40"
            class="inputbox"
            required="true"
            default=""
            format="%Y-%m-%d"
        />
        <field
            name="published"
            type="list"
            label="JSTATUS"
            description="COM_GONEWSLETTER_EDIT_PUBLISHED_DESC"
            class="inputbox"
            size="1"
            default="0">
            <option
                value="1">JPUBLISHED</option>
            <option
                value="0">JUNPUBLISHED</option>
        </field>
        <field
            type="file"
            name="pdf_file"
            label="COM_GONEWSLETTER_EDIT_FILE_LABEL"
            default=""
            description="COM_GONEWSLETTER_EDIT_FILE_DESC"
            size="40"
            accept="application/pdf"
            class="fileuploader"
        />
        <field
            name="file"
            type="hidden"
        />
    </fieldset>
</form>

and /administrator/components/com_comp_name/controllers/edit.php

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla controllerform library
jimport('joomla.application.component.controllerform');

/**
 * GoNewsletter Controller
 */
class GoNewsletterControllerEdit extends JControllerForm
{
    function __construct($config = array()) {
        $this->view_list = 'List';
        parent::__construct($config);
    }

    function save(){
        // ---------------------------- Uploading the file ---------------------
        // Neccesary libraries and variables
        jimport( 'joomla.filesystem.folder' );
        jimport('joomla.filesystem.file');
        $data = JRequest::getVar( 'jform', null, 'post', 'array' );

        // Create the gonewsleter folder if not exists in images folder
        if ( !JFolder::exists( JPATH_SITE . DS . "images" . DS . "gonewsletter" ) ) {
            JFolder::create( JPATH_SITE . DS . "images" . DS . "gonewsletter" );
        }

        // Get the file data array from the request.
        $file = JRequest::getVar( 'jform', null, 'files', 'array' );

        // Make the file name safe.
        $filename = JFile::makeSafe($file['name']['pdf_file']);

        // Move the uploaded file into a permanent location.
        if ( $filename != '' ) {
            // Make sure that the full file path is safe.
            $filepath = JPath::clean( JPATH_SITE . DS . 'images' . DS . 'gonewsletter' . DS . strtolower( $filename ) );

            // Move the uploaded file.
            JFile::upload( $file['tmp_name']['pdf_file'], $filepath );
            // Change $data['file'] value before save into the database 
            $data['file'] = strtolower( $filename );
        }
        // ---------------------------- File Upload Ends ------------------------

        JRequest::setVar('jform', $data );

        return parent::save();
    }

}

If you print out the $data before send it to parent::save($data) it contains the right fields you want to save, but it doesn't. I tried to use an input type=text instead of type=file and it saves correctly.

I tried another way like: input type=file and name=pdf_file, after then I added a hidden field name=file default="". And then I've set up this hidden field value to filename without success. Maybe I was doing something wrong. Keep continue to figure out something.

like image 97
Andras Gera Avatar answered Jan 16 '23 03:01

Andras Gera