Add File Uploader to Joomla Admin Component

折月煮酒 提交于 2019-12-10 10:19:42

问题


I made Joomla admin component according to Joomla guide - http://docs.joomla.org/Developing_a_Model-View-Controller_Component/2.5/Developing_a_Basic_Component

In that i need to have file uploader which let user to upload single file.

In administrator\components\com_invoicemanager\models\forms\invoicemanager.xml i have defined

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

In the controller administrator\components\com_invoicemanager\controllers\invoicemanager.php im trying to retrieve that file like below. But its not working (can't retrieve file)

Where am i doing it wrong ?

How can i get file and save it on disk ?

class InvoiceManagerControllerInvoiceManager extends JControllerForm
{
    function save(){
        $file = JRequest::getVar( 'invoice', '', 'files', 'array' );
        var_dump($file);
        exit(0);
    }
}

回答1:


make sure that you have included enctype="multipart/form-data" in the form that the file is being submitting. This is a common mistake

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

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

/// Move the uploaded file into a permanent location.
if (isset( $file['name'] )) {

/// Make sure that the full file path is safe.
$filepath = JPath::clean( $somepath.'/'.strtolower( $file['name'] ) );

/// Move the uploaded file.
JFile::upload( $file['tmp_name'], $filepath );}



回答2:


Think i found the solution :)

$file = JRequest::getVar('jform', null, 'files', 'array'); 

Saving part is mentioned here - http://docs.joomla.org/Secure_coding_guidelines




回答3:


For uploading the file from your component, you need to write your code in the controller file and you can extend the save() method. check the code given below -

public function save($data = array(), $key = 'id')
{
    // Neccesary libraries and variables
    jimport('joomla.filesystem.file');

    //Debugging
    ini_set("display_error" , 1);
    error_reporting(E_ALL);

    // Get input object
    $jinput = JFactory::getApplication()->input;

    // Get posted data
    $data  = $jinput->get('jform', null, 'raw');
    $file  = $jinput->files->get('jform');

    // renaming the file
    $file_ext=explode('.',JFile::makeSafe($file['invoice']['name'])); // invoice - file handler name
    $filename = round(microtime(true)) . '.' . strtolower(end($file_ext));
    // Move the uploaded file into a permanent location.
    if ( $filename != '' ) {

        // Make sure that the full file path is safe.
        $filepath = JPath::clean( JPATH_ROOT."/media/your_component_name/files/". $filename );

        // Move the uploaded file.
        if (JFile::upload( $file['invoice']['tmp_name'], $filepath )) {
              echo "success :)";
        } else {
              echo "failed :(";
        }

        $data['name'] =  $filename ; // getting file name
        $data['path'] =  $filepath ; // getting file path
        $data['size'] =  $file['invoice']['size'] ; // getting file size
    }
    JRequest::setVar('jform', $data, 'post');
    $return = parent::save($data);
    return $return;
}



回答4:


Joomla 2.5 & 3 style:

$app = JFactory::getApplication();
$input = $app->input;
$file= $input->files->get('file');

if(isset($file['name']))
{
    jimport('joomla.filesystem.file');
    $file['name'] = strtolower(JFile::makeSafe($file['name']));
    $fileRelativePath = '/pathToTheRightFolder/'.$file['name'];
    $fileAbsolutePath = JPath::clean( JPATH_ROOT.$fileRelativePath);
    JFile::upload( $file['tmp_name'], $fileAbsolutePath );
}



回答5:


http://docs.joomla.org/How_to_use_the_filesystem_package

has a full upload sample.

Little sample where admin choose the file type or all, enter the users to access the form upload. Folder to upload files in Joomla directory or with absolute path. Only selected users access the form upload.



来源:https://stackoverflow.com/questions/12909324/add-file-uploader-to-joomla-admin-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!