在Joomla模块中实现文件上传

6

我已经搜索了相当长的时间,但找不到符合我的需求的解决方案。我正在开发一个Joomla 2.5模块。我需要实现的功能是允许用户在模块配置中从后台上传图像/任何文件类型。

问题:如何在Joomla模块中添加文件上传字段。

谢谢!

2个回答

7

以下是 Joomla 文档的示例:

<?php

//Retrieve file details from uploaded file, sent from upload form
$file = JRequest::getVar('file_upload', 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 . DS . "uploads" . DS . $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) ) {
      header("Location: http://".$_SERVER["HTTP_HOST"]."/administrator"); Redirect to a page of your choice
   } else {
     echo "error !"; //Redirect and throw an error message
   }
} else {
   echo "Wrong extension !"; //Redirect and notify user file is not right extension
}

?>

如果需要更详细的信息和完整的示例,请访问: http://docs.joomla.org/How_to_use_the_filesystem_package


此链接涉及IT技术相关内容。

6
请记住,你的HTML表单必须包括 enctype="multipart/form-data" ,否则你将无法使用joomla函数获得任何结果!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接