我在为基于库/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但它忽略了上传代码应该去哪里?
非常感谢