繁体   English   中英

Joomla开发文件处理

[英]Joomla Development File Handling

在大多数情况下,我将按照Hello World教程为joomla创建一个简单的MVC组件,其中包含一些文本字段和图像。

文本字段保存,但“文件”字段不保存,有什么想法吗?

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

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

    /**
     * MJob Controller
     */
    class MJobsControllerMJob extends JControllerForm
    {
    }

**Model:**

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

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

    class MJobsModelMJob extends JModelAdmin
    {
        public function getTable($type = 'MJob', $prefix = 'MJobsTable', $config = array()) 
        {
            return JTable::getInstance($type, $prefix, $config);
        }

        public function getForm($data = array(), $loadData = true) 
        {
            // Get the form.
            $form = $this->loadForm('com_mjobs.mjob', 'mjob', array('control' => 'jform', 'load_data' => $loadData));

            if (empty($form)) 
            {
                return false;
            }
            return $form;
        }

        protected function loadFormData() 
        {
            // Check the session for previously entered form data.
            $data = JFactory::getApplication()->getUserState('com_mjobs.edit.mjob.data', array());
            if (empty($data)) 
            {
                $data = $this->getItem();
            }
            return $data;
        }
    }

**view.html.php:**

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

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

    class MJobsViewMJob extends JView
    {

        public function display($tpl = null) 
        {
            // get the Data
            $form = $this->get('Form');
            $item = $this->get('Item');

            // Check for errors.
            if (count($errors = $this->get('Errors'))) 
            {
                JError::raiseError(500, implode('<br />', $errors));
                return false;
            }
            // Assign the Data
            $this->form = $form;
            $this->item = $item;

            // Set the toolbar
            $this->addToolBar();

            // Display the template
            parent::display($tpl);
        }

        protected function addToolBar() 
        {
            JRequest::setVar('hidemainmenu', true);
            $isNew = ($this->item->id == 0);
            JToolBarHelper::title($isNew ? JText::_('COM_MJOBS_MANAGER_MJOB_NEW') : JText::_('COM_MJOBS_MANAGER_MJOB_EDIT'));
            JToolBarHelper::apply('mjob.apply');
            JToolBarHelper::save('mjob.save');
            JToolBarHelper::save2new('mjob.save2new');
            JToolBarHelper::cancel('mjob.cancel', $isNew ? 'JTOOLBAR_CANCEL' : 'JTOOLBAR_CLOSE');
        }
    }

**VIEW (tmpl/edit.php):**
<?php
// No direct access
defined('_JEXEC') or die('Restricted access');
JHtml::_('behavior.tooltip');
echo "<b>MJOB EDIT START</b><br>";
?>
<form action="<?php echo JRoute::_('index.php?option=com_mjobs&layout=edit&id='.(int) $this->item->id); ?>"
      method="post" enctype="multipart/form-data" name="adminForm" id="mjob-form">
    <fieldset class="adminform">
        <legend><?php echo JText::_( 'COM_MJOBS_MJOB_DETAILS' ); ?></legend>
        <ul class="adminformlist">
            <?php foreach($this->form->getFieldset('details') as $field): ?>
                <li><?php echo $field->label;echo $field->input;?></li>
            <?php endforeach; ?>
        </ul>
    </fieldset>
    <div>
        <input type="hidden" name="task" value="mjob.edit" />
        <?php echo JHtml::_('form.token'); ?>
    </div>
</form>

我猜您正在使用Joomla! 2.5根据提供的代码。

因此,要检索已上传的文件,您将需要执行以下操作:

$jFileInput = new JInput($_FILES);
$theFile = $jFileInput->get('jform',array(),'array');


// If there is no uploaded file, we have a problem...
if (!is_array($theFile)) {
    JError::raiseWarning('', 'No file was selected.');
    return false;
}

// Build the paths for our file to move to the components 'upload' directory
$theFileName = $theFile['name']['tablefile'];
$tmp_src    = $theFile['tmp_name']['tablefile'];
$tmp_dest   = JPATH_COMPONENT_ADMINISTRATOR . '/uploads/' . $theFileName;
$this->dataFile = $theFileName;

// Move uploaded file
jimport('joomla.filesystem.file');
$uploaded = JFile::upload($tmp_src, $tmp_dest);
// $uploaded contains boolean indicating success or failure
// $tmp_dest will contain final location of file if successful.

另外,请确保您拥有html表单

enctype =“ multipart / form-data”

启用,否则您将不会在php中收到任何文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM