0

我不知道为什么 Joomla 不允许通过 .xml 创建的表单上传图像。

我有一个文件上传字段

<field name="nuotrauka" type="file"
label="COM_DALYVIAI_FORM_LBL_DALYVIS_NUOTRAUKA"
description="COM_DALYVIAI_FORM_DESC_DALYVIS_NUOTRAUKA" 
upload_directory="/images/"
accept="image/*" /> 

表单提交后我收到错误:“错误:不允许此文件类型”

我尝试了 .jpg、.png 文件类型。

4

1 回答 1

0

问题是 Joomla 文件字段只不过是一种生成表单字段以选择文件的方法。这意味着在提交表单时,核心中没有内置逻辑来处理服务器端的文件提交。您需要向组件控制器添加逻辑以从请求中获取信息并将其保存在需要的位置。我从需要上传文件的项目控制器中粘贴了一个示例方法。

public function upload() {
    //JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
    $jinput = JFactory::getApplication()->input;
    jimport('joomla.filesystem.file');

    // We're putting all our files in a directory called images.
    $path = 'images/uploaded/';

    // The posted data, for reference
    $file = $jinput->get('value', '', 'string');
    $name = $jinput->get('name', '', 'string');

    // Get the mime
    $getMime = explode('.', $name);
    $mime = end($getMime);

    // Separate out the data
    $data = explode(',', $file);

    echo "<h1>" . $path . $name . "</h1>";

    // Encode it correctly
    $encodedData = str_replace(' ','+',$data[1]);
    $decodedData = base64_decode($encodedData);
    //if (JFile::upload($decodedData, $path . $name)) {
    if(file_put_contents($path . $name, $decodedData)) {
        JError::raiseNotice(null, $name . ": uploaded successfully");
    } else {
        // Show an error message should something go wrong.
        JError::raiseError(null, "Something went wrong. Check that the file isn't corrupted");
    }
}

如果您已经编写了服务器端文件上传控制器方法并且您已经确认所有其他建议都不是问题,那么您应该在问题中包含代码,因为这很可能是发生错误的地方。

于 2014-09-02T22:46:35.130 回答