2012-06-03 70 views
6

我一直在尋找這個相當長的一段時間,但找不到解決方案來匹配我的需求。我正在爲Joomla 2.5開發一個模塊。我需要功能來允許用戶在模塊配置中從後端上載圖像/任何文件類型。joomla模塊中的文件上傳

問題:如何在joomla模塊中添加文件上傳字段。

謝謝!

+0

有什麼問題? – jzeus

回答

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 
} 

?> 

有關詳細信息和完整的例子(S):http://docs.joomla.org/How_to_use_the_filesystem_package

6

請記住,你的HTML表單必須包括

enctype="multipart/form-data"

否則你不會得到任何結果的joomla功能!