Есть несколько вещей, которые нужно настроить, чтобы загрузка работала.
- Изменить энтип формы на
enctype="multipart/form-data"
- Если используется архитектура MVC, добавьте
task
и controller
в форму.
Загрузка файла довольно проста, потому что Joomla имеет пакет filesystem
. Все, что вам нужно, чтобы загрузить файл, это позвонить JFile::upload($src, $dest)
.
Читайте о пакете файловой системы, вы найдете много полезного. Вот ссылка http://docs.joomla.org/How_to_use_the_filesystem_package
Вот как выглядит загружаемый код (из документации Joomla)
/**
* 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;
}