dongyin8991 2012-05-23 18:38
浏览 90
已采纳

Joomla开发文件处理

I'm making a simple MVC component for joomla following the hello world tutorial for the most part, with some some text fields and an image.

The text fields save but the "file" field does not, any ideas?

**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>
  • 写回答

2条回答 默认 最新

  • doukesou4452 2012-05-23 22:11
    关注

    I'm guessing you're using Joomla! 2.5 based on the code provided.

    So to retrieve a file that's been uploaded you will need to do something like:

    $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.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻看一个题
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)