Joomla-如何在模板参数中添加按钮上传文件并上传文件? [英] Joomla - how to add abutton upload file to parameter of template and do upload file?
问题描述
我自定义参数以添加按钮上传<input type="file" name="file">
i custom a parameter to add a button upload <input type="file" name="file">
但是当我们处于com_template时如何上传
But how to upload when we are in com_template
推荐答案
需要进行一些调整才能使上传正常进行.
There are couple of things you need to tweak to make the upload work.
- 将表单的enctype更改为
enctype="multipart/form-data"
- 如果使用MVC架构,请在表单中添加
task
和controller
.
- Change form's enctype to
enctype="multipart/form-data"
- If using MVC architecture, add
task
andcontroller
to the form.
文件上传非常容易,因为Joomla已安装了filesystem
软件包.您只需上传文件即可调用JFile::upload($src, $dest)
.
Uploading of file is pretty easy, because Joomla has filesystem
package in place. All you need to upload the file is to call JFile::upload($src, $dest)
.
阅读有关文件系统软件包的信息,您会发现很多有用的东西.这是链接 http://docs.joomla.org/How_to_use_the_filesystem_package
Read about filesystem package, you will find a lot of useful stuff. Here is the link http://docs.joomla.org/How_to_use_the_filesystem_package
这是上传代码的样子(来自Joomla文档)
Here is what uploading code looks like (from Joomla documentation)
/**
* Uploading function for Joomla
* @param int $max maximum allowed site of file
* @param string $module_dir path to where to upload file
* @param string $file_type allowed file type
* @return string response message
*/
function fileUpload($max, $module_dir, $file_type){
//Retrieve file details from uploaded file, sent from upload form
$file = JRequest::getVar('file_upload', null, 'files', 'array');
// Retorna: Array ( [name] => mod_simpleupload_1.2.1.zip [type] => application/zip
// [tmp_name] => /tmp/phpo3VG9F [error] => 0 [size] => 4463 )
if(isset($file)){
//Clean up filename to get rid of strange characters like spaces etc
$filename = JFile::makeSafe($file['name']);
if($file['size'] > $max) $msg = JText::_('ONLY_FILES_UNDER').' '.$max;
//Set up the source and destination of the file
$src = $file['tmp_name'];
$dest = $module_dir . DS . $filename;
//First check if the file has the right extension, we need jpg only
if ($file['type'] == $file_type || $file_type == '*') {
if ( JFile::upload($src, $dest) ) {
//Redirect to a page of your choice
$msg = JText::_('FILE_SAVE_AS').' '.$dest;
} else {
//Redirect and throw an error message
$msg = JText::_('ERROR_IN_UPLOAD');
}
} else {
//Redirect and notify user file is not right extension
$msg = JText::_('FILE_TYPE_INVALID');
}
}
return $msg;
}
这篇关于Joomla-如何在模板参数中添加按钮上传文件并上传文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!