2

我在为基于库/joomla/filesystem/file.php 的自定义字段创建上传机制时遇到问题。表单在视图中设置了正确的 enctype,它只是没有上传。这是我的组件/模型/字段/uploadfield.php 中的代码:

protected function getInput()
{

    //Retrieve file details from uploaded file, sent from upload form
    $file = JFactory::getApplication()->input->get($this->name, null, 'files', 'array');

    //Import filesystem libraries. Perhaps not necessary, but does not hurt
    jimport('joomla.filesystem.file');

    //Clean up filename to get rid of strange characters like spaces etc
    $filename = JFile::makeSafe($file['name']);

    //Set up the source and destination of the file
    $src = $file['tmp_name'];
    $dest = JPATH_COMPONENT . DIRECTORY_SEPARATOR . $filename;

    //First check if the file has the right extension, we need jpg only
    if ( strtolower(JFile::getExt($filename) ) == 'jpg') {
        if ( JFile::upload($src, $dest) ) {
        //Redirect to a page of your choice
        } else {
        //Redirect and throw an error message
        }
    } else {
    //Redirect and notify user file is not right extension
    }

    return '<input type="file" name="' . $this->name . '" id="' . $this->id . '"' . ' />';  
}

在 getInput() 函数中使用上传机制,我什至会以正确的方式解决这个问题吗?它应该在模型中吗?我真的很困惑如何使这项工作,一直试图遵循:http ://docs.joomla.org/How_to_use_the_filesystem_package但它忽略了上传代码应该去哪里?

非常感谢

4

2 回答 2

3

尝试使用我在其中一个组件上使用的以下内容:

function getInput(){

    jimport('joomla.filesystem.file');

    $jinput = JFactory::getApplication()->input;
    $fileInput = new JInput($_FILES);
    $file = $fileInput->get('image', null, 'array');

    if(isset($file) && !empty($file['name'])) { 
        $filename = JFile::makeSafe($file['name']);
        $src = $file['tmp_name'];
        $data['image']=$filename;

        $dest = JPATH_COMPONENT . '/' . $filename;

        if ( strtolower(JFile::getExt($filename) ) == 'jpg') {
            if(!JFile::upload($src, $dest)) {
            return false;
            }
        }
        else {
            JError::raiseWarning('', 'File type not allowed!.');
            return false;
        }
    }
    return true;
}

请注意,使用以下代码:

$file = JRequest::getVar('image', null, 'files', 'array');

image ”来自输入字段的名称,如下所示:

<input type="file" id="" name="image" />

因此,更改为您在输入字段中指定的任何名称。

于 2012-12-12T16:08:49.190 回答
1

好的 - 所以我解决了这个问题:

我使用自定义字段文件中的 getInput() 函数来完成在表单上显示输入字段的工作,并在控制器中创建了一个函数来进行保存。有点像这样:

function save(){
    jimport('joomla.filesystem.file');

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

    $filepath = "DESTINATIONFILEPATH"
    JFile::upload( $theFiles['tmp_name']['filename'], $filepath );

    return parent::save();
}

只需要找出将文件名保存到 JInput "jform" 数组的 Joomla 3.0 方法,以便将文件名更新到数据库中。这只是覆盖现有的 jform 数据:

$jinput = JFactory::getApplication()->input;
$jinput->set('jform',$arraytoadd);

如果有人感兴趣,我在这里问过这个问题: Adding field input to JForm using JInput

于 2013-01-03T18:56:52.560 回答