【问题标题】:File upload form for custom Joomla component自定义 Joomla 组件的文件上传表单
【发布时间】:2013-03-17 05:06:59
【问题描述】:

我的表单中有一个文件类型的字段。当用户点击保存图标时,我自然想将文件上传到服务器,并将文件名保存在数据库中。我尝试通过回显文件名来测试它,但它似乎不起作用。另外,如何将文件名添加到数据库中?是在模型中完成的吗?谢谢!

控制器/customcom.php

jimport('joomla.filesystem.file');     
class CustomComControllerCustomCom extends JControllerForm
        {
            function save()
            {
                $file = JRequest::getVar('img_url', null, 'files', 'array');

                $filename = JFile::makeSafe($file['name']);

                echo $filename;
                }
        }

models/forms/customcom.xml

    <?xml version="1.0" encoding="utf-8"?>
    <form enctype="multipart/form-data">
            <fieldset>
                  <field
                        name="img_url"
                        type="file"
                        label="Image upload"
                        description=""
                        size="40"
                        class="inputbox"
                        default=""
                />
            </fieldset>
   </form>

【问题讨论】:

    标签: file file-upload joomla joomla2.5


    【解决方案1】:

    刚刚想通了。

    正确的做法是

    $jinput = JFactory::getApplication()->input;
    $files = $jinput->files->get('jform');
    $file = $files['img_url'];
    

    这应该可以解决问题。 然后 $file 数组包含以下键:

    • 错误
    • 姓名
    • 尺寸
    • tmp_name
    • 类型

    我删除了我原来的答案,因为它具有误导性。

    【讨论】:

      【解决方案2】:

      从媒体经理那里获得灵感:

      请注意,这个组件已经很老了(正在准备重构)

      // Import dependencies
      jimport('joomla.filesystem.file')
      
      // Get input
      $input = JFactory::getApplication()->input;
      
      // Get uploaded file info
      $file_info = $input->files->get('img_url', null);
      
      // This will hold error
      $error = null;
      
      // Check if there was upload at all, mime is correct, file size, XSS, whatever...
      if (!MycomponentHelper::canUpload($file_info, $error)
      {
          $this->setError('problem: ' . $error);
          return false;
      }
      
      // Move uploaded file destination
      JFile::upload($file_info['tmp_name'], $destination);
      

      【讨论】:

        猜你喜欢
        • 2012-11-30
        • 2015-08-22
        • 2013-07-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-15
        • 1970-01-01
        相关资源
        最近更新 更多