如何使用JInput获取文件tmp_name

时间:2013-03-28 05:54:59

标签: file-upload joomla save joomla2.5 jinput

我有点困惑。我有一些代码,它设法获取我的文件的文件名:

class AControllerA extends JControllerForm
{
    function save()
    {
        //Upload file
        jimport('joomla.filesystem.file');
        $jinput = JFactory::getApplication()->input;
        $store_form = $jinput->get('jform', null, 'array');
            $file = $store_form['img_url'];
        echo $file;
     }
}

*文件字段的名称为jform [img_url];

但是我似乎无法获得该文件的'tmp_name'。谁知道我错过了什么?我对jinput的运作方式感到有些困惑...... jrequest很容易。谢谢!

模型/形式/ A.XML

<form enctype="multipart/form-data">
        <fieldset>
        <field
                        name="img_url"
                        type="file"
                        label=""
                        description=""
                        size="40"
                        class="inputbox"
                        default=""
                />
       </fieldset>
</form>

3 个答案:

答案 0 :(得分:5)

这样怎么样:

$files = $input->files->get('jform', null);
$filename = $files['img_url']['tmp_name'];
echo $filename;

查看Retrieving file data using JInput

的文档

答案 1 :(得分:3)

假设您正在使用JForm和文件输入类型,那么您可以使用以下命令访问该文件:

$files = $jinput->files->get('jform');
$file = $files['img_url']['tmp_name']

另外,请确保您的表单设置了enctype="multipart/form-data",否则无效。

答案 2 :(得分:2)

在你的模特中你应该有这样的

 public function getForm($data = array(), $loadData = false)
    {
        /**
         * Get the Form
         */
        $form = $this->loadForm('com_mycomponent.mycomponent', 'mycomponent',
                                        array('control' => false, 'load_data' => $loadData));
        if (empty($form)) {
            return false;
        }
        return $form;
    }

请注意,$ loaddata和'control'设置为false,当'control'为false时,您可以根据xml中指定的名称获取文件参数,即输出形式如下:

<input name="name in xml file" type="file" />

如果'control'=&gt; 'jform'

<input name="jform[name in xml file]" type="file" />

$ loaddata = false表示您不需要从数据库中获取任何数据到表单。

你的view.html.php中的

你应该像这样

public function display($tpl = null)
    {
        $this->formData = $this->get('Form');
        $this->addToolbar();
        parent::display($tpl);
    }

假设我将在“mycomponent”控制器的“upload”方法中收到所请求的文件,那么它应该是这样的:

class MycomponentControllerMycomponent extends JControllerAdmin
{
public function upload()
    {
        //Retrieve file details from uploaded file, sent from upload form
        $file = JFactory::getApplication()->input->files->get('name in xml 
        **$tmp_name** = $file['tmp_name'];

    }
}

$ tmp_name是您的必需名称